
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>osxphotos.photoinfo._photoinfo_export &#8212; osxphotos 0.43.6 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/alabaster.css" />
    <script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
   
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for osxphotos.photoinfo._photoinfo_export</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot; Export methods for PhotoInfo </span>
<span class="sd">    The following methods are defined and must be imported into PhotoInfo as instance methods:</span>
<span class="sd">    export</span>
<span class="sd">    export2</span>
<span class="sd">    _export_photo</span>
<span class="sd">    _write_exif_data</span>
<span class="sd">    _exiftool_json_sidecar</span>
<span class="sd">    _get_exif_keywords</span>
<span class="sd">    _get_exif_persons</span>
<span class="sd">    _exiftool_dict</span>
<span class="sd">    _xmp_sidecar</span>
<span class="sd">    _write_sidecar</span>
<span class="sd">    &quot;&quot;&quot;</span>

<span class="c1"># TODO: should this be its own PhotoExporter class?</span>
<span class="c1"># TODO: the various sidecar_json, sidecar_xmp, etc args should all be collapsed to a sidecar param using a bit mask</span>

<span class="kn">import</span> <span class="nn">dataclasses</span>
<span class="kn">import</span> <span class="nn">glob</span>
<span class="kn">import</span> <span class="nn">hashlib</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">pathlib</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">tempfile</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">namedtuple</span>  <span class="c1"># pylint: disable=syntax-error</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>

<span class="kn">import</span> <span class="nn">photoscript</span>
<span class="kn">from</span> <span class="nn">mako.template</span> <span class="kn">import</span> <span class="n">Template</span>

<span class="kn">from</span> <span class="nn">.._constants</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">_MAX_IPTC_KEYWORD_LEN</span><span class="p">,</span>
    <span class="n">_OSXPHOTOS_NONE_SENTINEL</span><span class="p">,</span>
    <span class="n">_TEMPLATE_DIR</span><span class="p">,</span>
    <span class="n">_UNKNOWN_PERSON</span><span class="p">,</span>
    <span class="n">_XMP_TEMPLATE_NAME</span><span class="p">,</span>
    <span class="n">_XMP_TEMPLATE_NAME_BETA</span><span class="p">,</span>
    <span class="n">DEFAULT_PREVIEW_SUFFIX</span><span class="p">,</span>
    <span class="n">LIVE_VIDEO_EXTENSIONS</span><span class="p">,</span>
    <span class="n">SIDECAR_EXIFTOOL</span><span class="p">,</span>
    <span class="n">SIDECAR_JSON</span><span class="p">,</span>
    <span class="n">SIDECAR_XMP</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">.._version</span> <span class="kn">import</span> <span class="n">__version__</span>
<span class="kn">from</span> <span class="nn">..datetime_utils</span> <span class="kn">import</span> <span class="n">datetime_tz_to_utc</span>
<span class="kn">from</span> <span class="nn">..exiftool</span> <span class="kn">import</span> <span class="n">ExifTool</span>
<span class="kn">from</span> <span class="nn">..export_db</span> <span class="kn">import</span> <span class="n">ExportDBNoOp</span>
<span class="kn">from</span> <span class="nn">..fileutil</span> <span class="kn">import</span> <span class="n">FileUtil</span>
<span class="kn">from</span> <span class="nn">..photokit</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">PHOTOS_VERSION_CURRENT</span><span class="p">,</span>
    <span class="n">PHOTOS_VERSION_ORIGINAL</span><span class="p">,</span>
    <span class="n">PhotoKitFetchFailed</span><span class="p">,</span>
    <span class="n">PhotoLibrary</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">..phototemplate</span> <span class="kn">import</span> <span class="n">RenderOptions</span>
<span class="kn">from</span> <span class="nn">..uti</span> <span class="kn">import</span> <span class="n">get_preferred_uti_extension</span>
<span class="kn">from</span> <span class="nn">..utils</span> <span class="kn">import</span> <span class="n">increment_filename</span><span class="p">,</span> <span class="n">increment_filename_with_count</span><span class="p">,</span> <span class="n">lineno</span>

<span class="c1"># retry if use_photos_export fails the first time (which sometimes it does)</span>
<span class="n">MAX_PHOTOSCRIPT_RETRIES</span> <span class="o">=</span> <span class="mi">3</span>


<span class="k">class</span> <span class="nc">ExportError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;error during export&quot;&quot;&quot;</span>

    <span class="k">pass</span>


<span class="k">class</span> <span class="nc">ExportResults</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;holds export results for export2&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">exported</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">new</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">updated</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">skipped</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">exif_updated</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">touched</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">converted_to_jpeg</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">sidecar_json_written</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">sidecar_json_skipped</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">sidecar_exiftool_written</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">sidecar_exiftool_skipped</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">sidecar_xmp_written</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">sidecar_xmp_skipped</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">missing</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">error</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">exiftool_warning</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">exiftool_error</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">xattr_written</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">xattr_skipped</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">deleted_files</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">deleted_directories</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">exported_album</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">skipped_album</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">missing_album</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exported</span> <span class="o">=</span> <span class="n">exported</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">new</span> <span class="o">=</span> <span class="n">new</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">updated</span> <span class="o">=</span> <span class="n">updated</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">skipped</span> <span class="o">=</span> <span class="n">skipped</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exif_updated</span> <span class="o">=</span> <span class="n">exif_updated</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">touched</span> <span class="o">=</span> <span class="n">touched</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">converted_to_jpeg</span> <span class="o">=</span> <span class="n">converted_to_jpeg</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_json_written</span> <span class="o">=</span> <span class="n">sidecar_json_written</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_json_skipped</span> <span class="o">=</span> <span class="n">sidecar_json_skipped</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_exiftool_written</span> <span class="o">=</span> <span class="n">sidecar_exiftool_written</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_exiftool_skipped</span> <span class="o">=</span> <span class="n">sidecar_exiftool_skipped</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_xmp_written</span> <span class="o">=</span> <span class="n">sidecar_xmp_written</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_xmp_skipped</span> <span class="o">=</span> <span class="n">sidecar_xmp_skipped</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">missing</span> <span class="o">=</span> <span class="n">missing</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">error</span> <span class="o">=</span> <span class="n">error</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exiftool_warning</span> <span class="o">=</span> <span class="n">exiftool_warning</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exiftool_error</span> <span class="o">=</span> <span class="n">exiftool_error</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">xattr_written</span> <span class="o">=</span> <span class="n">xattr_written</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">xattr_skipped</span> <span class="o">=</span> <span class="n">xattr_skipped</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">deleted_files</span> <span class="o">=</span> <span class="n">deleted_files</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">deleted_directories</span> <span class="o">=</span> <span class="n">deleted_directories</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exported_album</span> <span class="o">=</span> <span class="n">exported_album</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">skipped_album</span> <span class="o">=</span> <span class="n">skipped_album</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">missing_album</span> <span class="o">=</span> <span class="n">missing_album</span> <span class="ow">or</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">all_files</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;return all filenames contained in results&quot;&quot;&quot;</span>
        <span class="n">files</span> <span class="o">=</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">exported</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">new</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">updated</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">skipped</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">exif_updated</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">touched</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">converted_to_jpeg</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_json_written</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_json_skipped</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_exiftool_written</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_exiftool_skipped</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_xmp_written</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_xmp_skipped</span>
            <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">missing</span>
        <span class="p">)</span>
        <span class="n">files</span> <span class="o">+=</span> <span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">exiftool_warning</span><span class="p">]</span>
        <span class="n">files</span> <span class="o">+=</span> <span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">exiftool_error</span><span class="p">]</span>
        <span class="n">files</span> <span class="o">+=</span> <span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">error</span><span class="p">]</span>

        <span class="n">files</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">files</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">files</span>

    <span class="k">def</span> <span class="fm">__iadd__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exported</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">exported</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">new</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">new</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">updated</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">updated</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">skipped</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">skipped</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exif_updated</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">exif_updated</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">touched</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">touched</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">converted_to_jpeg</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">converted_to_jpeg</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_json_written</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">sidecar_json_written</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_json_skipped</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">sidecar_json_skipped</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_exiftool_written</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">sidecar_exiftool_written</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_exiftool_skipped</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">sidecar_exiftool_skipped</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_xmp_written</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">sidecar_xmp_written</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sidecar_xmp_skipped</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">sidecar_xmp_skipped</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">missing</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">missing</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">error</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">error</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exiftool_warning</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">exiftool_warning</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exiftool_error</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">exiftool_error</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">deleted_files</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">deleted_files</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">deleted_directories</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">deleted_directories</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exported_album</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">exported_album</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">skipped_album</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">skipped_album</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">missing_album</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">missing_album</span>

        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span>
            <span class="s2">&quot;ExportResults(&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;exported=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">exported</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,new=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">new</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,updated=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">updated</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,skipped=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">skipped</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,exif_updated=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">exif_updated</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,touched=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">touched</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,converted_to_jpeg=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">converted_to_jpeg</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,sidecar_json_written=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">sidecar_json_written</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,sidecar_json_skipped=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">sidecar_json_skipped</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,sidecar_exiftool_written=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">sidecar_exiftool_written</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,sidecar_exiftool_skipped=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">sidecar_exiftool_skipped</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,sidecar_xmp_written=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">sidecar_xmp_written</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,sidecar_xmp_skipped=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">sidecar_xmp_skipped</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,missing=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">missing</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,error=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">error</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,exiftool_warning=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">exiftool_warning</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,exiftool_error=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">exiftool_error</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,deleted_files=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">deleted_files</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,deleted_directories=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">deleted_directories</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,exported_album=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">exported_album</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,skipped_album=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">skipped_album</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="sa">f</span><span class="s2">&quot;,missing_album=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">missing_album</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="o">+</span> <span class="s2">&quot;)&quot;</span>
        <span class="p">)</span>


<span class="c1"># hexdigest is not a class method, don&#39;t import this into PhotoInfo</span>
<span class="k">def</span> <span class="nf">hexdigest</span><span class="p">(</span><span class="n">strval</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;hexdigest of a string, using blake2b&quot;&quot;&quot;</span>
    <span class="n">h</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">blake2b</span><span class="p">(</span><span class="n">digest_size</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
    <span class="n">h</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="nb">bytes</span><span class="p">(</span><span class="n">strval</span><span class="p">,</span> <span class="s2">&quot;utf-8&quot;</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>


<span class="c1"># _export_photo_uuid_applescript is not a class method, don&#39;t import this into PhotoInfo</span>
<span class="k">def</span> <span class="nf">_export_photo_uuid_applescript</span><span class="p">(</span>
    <span class="n">uuid</span><span class="p">,</span>
    <span class="n">dest</span><span class="p">,</span>
    <span class="n">filestem</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">original</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">edited</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">live_photo</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">timeout</span><span class="o">=</span><span class="mi">120</span><span class="p">,</span>
    <span class="n">burst</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">dry_run</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">overwrite</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Export photo to dest path using applescript to control Photos</span>
<span class="sd">    If photo is a live photo, exports both the photo and associated .mov file</span>

<span class="sd">    Args:</span>
<span class="sd">        uuid: UUID of photo to export</span>
<span class="sd">        dest: destination path to export to</span>
<span class="sd">        filestem: (string) if provided, exported filename will be named stem.ext</span>
<span class="sd">                where ext is extension of the file exported by photos (e.g. .jpeg, .mov, etc)</span>
<span class="sd">                If not provided, file will be named with whatever name Photos uses</span>
<span class="sd">                If filestem.ext exists, it wil be overwritten</span>
<span class="sd">        original: (boolean) if True, export original image; default = True</span>
<span class="sd">        edited: (boolean) if True, export edited photo; default = False</span>
<span class="sd">                If photo not edited and edited=True, will still export the original image</span>
<span class="sd">                caller must verify image has been edited</span>
<span class="sd">        *Note*: must be called with either edited or original but not both,</span>
<span class="sd">                will raise error if called with both edited and original = True</span>
<span class="sd">        live_photo: (boolean) if True, export associated .mov live photo; default = False</span>
<span class="sd">        timeout: timeout value in seconds; export will fail if applescript run time exceeds timeout</span>
<span class="sd">        burst: (boolean) set to True if file is a burst image to avoid Photos export error</span>
<span class="sd">        dry_run: (boolean) set to True to run in &quot;dry run&quot; mode which will download file but not actually copy to destination</span>

<span class="sd">    Returns: list of paths to exported file(s) or None if export failed</span>

<span class="sd">    Raises: ExportError if error during export</span>

<span class="sd">    Note: For Live Photos, if edited=True, will export a jpeg but not the movie, even if photo</span>
<span class="sd">          has not been edited. This is due to how Photos Applescript interface works.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">dest</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">dest</span><span class="o">.</span><span class="n">is_dir</span><span class="p">():</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;dest </span><span class="si">{</span><span class="n">dest</span><span class="si">}</span><span class="s2"> must be a directory&quot;</span><span class="p">)</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">original</span> <span class="o">^</span> <span class="n">edited</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;edited or original must be True but not both&quot;</span><span class="p">)</span>

    <span class="n">tmpdir</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">TemporaryDirectory</span><span class="p">(</span><span class="n">prefix</span><span class="o">=</span><span class="s2">&quot;osxphotos_&quot;</span><span class="p">)</span>

    <span class="n">exported_files</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">filename</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="c1"># I&#39;ve seen intermittent failures with the PhotoScript export so retry if</span>
        <span class="c1"># export doesn&#39;t return anything</span>
        <span class="n">retries</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="ow">not</span> <span class="n">exported_files</span> <span class="ow">and</span> <span class="n">retries</span> <span class="o">&lt;</span> <span class="n">MAX_PHOTOSCRIPT_RETRIES</span><span class="p">:</span>
            <span class="n">photo</span> <span class="o">=</span> <span class="n">photoscript</span><span class="o">.</span><span class="n">Photo</span><span class="p">(</span><span class="n">uuid</span><span class="p">)</span>
            <span class="n">filename</span> <span class="o">=</span> <span class="n">photo</span><span class="o">.</span><span class="n">filename</span>
            <span class="n">exported_files</span> <span class="o">=</span> <span class="n">photo</span><span class="o">.</span><span class="n">export</span><span class="p">(</span>
                <span class="n">tmpdir</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">original</span><span class="o">=</span><span class="n">original</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span>
            <span class="p">)</span>
            <span class="n">retries</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ExportError</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">exported_files</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">filename</span><span class="p">:</span>
        <span class="c1"># nothing got exported</span>
        <span class="k">raise</span> <span class="n">ExportError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Could not export photo </span><span class="si">{</span><span class="n">uuid</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">)</span>

    <span class="c1"># need to find actual filename as sometimes Photos renames JPG to jpeg on export</span>
    <span class="c1"># may be more than one file exported (e.g. if Live Photo, Photos exports both .jpeg and .mov)</span>
    <span class="c1"># TemporaryDirectory will cleanup on return</span>
    <span class="n">filename_stem</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span><span class="o">.</span><span class="n">stem</span>
    <span class="n">exported_paths</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">fname</span> <span class="ow">in</span> <span class="n">exported_files</span><span class="p">:</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">tmpdir</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="o">/</span> <span class="n">fname</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">exported_files</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">live_photo</span> <span class="ow">and</span> <span class="n">path</span><span class="o">.</span><span class="n">suffix</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s2">&quot;.mov&quot;</span><span class="p">:</span>
            <span class="c1"># it&#39;s the .mov part of live photo but not requested, so don&#39;t export</span>
            <span class="k">continue</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">exported_files</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">burst</span> <span class="ow">and</span> <span class="n">path</span><span class="o">.</span><span class="n">stem</span> <span class="o">!=</span> <span class="n">filename_stem</span><span class="p">:</span>
            <span class="c1"># skip any burst photo that&#39;s not the one we asked for</span>
            <span class="k">continue</span>
        <span class="k">if</span> <span class="n">filestem</span><span class="p">:</span>
            <span class="c1"># rename the file based on filestem, keeping original extension</span>
            <span class="n">dest_new</span> <span class="o">=</span> <span class="n">dest</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">filestem</span><span class="si">}{</span><span class="n">path</span><span class="o">.</span><span class="n">suffix</span><span class="si">}</span><span class="s2">&quot;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># use the name Photos provided</span>
            <span class="n">dest_new</span> <span class="o">=</span> <span class="n">dest</span> <span class="o">/</span> <span class="n">path</span><span class="o">.</span><span class="n">name</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">dry_run</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">overwrite</span> <span class="ow">and</span> <span class="n">dest_new</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
                <span class="n">FileUtil</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">dest_new</span><span class="p">)</span>
            <span class="n">FileUtil</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">path</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">dest_new</span><span class="p">))</span>
        <span class="n">exported_paths</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">dest_new</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">exported_paths</span>


<span class="c1"># _check_export_suffix is not a class method, don&#39;t import this into PhotoInfo</span>
<span class="k">def</span> <span class="nf">_check_export_suffix</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">edited</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Helper function for exporting photos to check file extensions of destination path.</span>

<span class="sd">    Checks that dst file extension is appropriate for the src.</span>
<span class="sd">    If edited=True, will use src file extension of &quot;.jpeg&quot; if None provided for src.</span>

<span class="sd">    Args:</span>
<span class="sd">        src: path to source file or None.</span>
<span class="sd">        dest: path to destination file.</span>
<span class="sd">        edited: set to True if exporting an edited photo.</span>

<span class="sd">    Returns:</span>
<span class="sd">        True if src and dest extensions are OK, else False.</span>

<span class="sd">    Raises:</span>
<span class="sd">        ValueError if edited is False and src is None</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># check extension of destination</span>
    <span class="k">if</span> <span class="n">src</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="c1"># use suffix from edited file</span>
        <span class="n">actual_suffix</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">src</span><span class="p">)</span><span class="o">.</span><span class="n">suffix</span>
    <span class="k">elif</span> <span class="n">edited</span><span class="p">:</span>
        <span class="c1"># use .jpeg as that&#39;s probably correct</span>
        <span class="n">actual_suffix</span> <span class="o">=</span> <span class="s2">&quot;.jpeg&quot;</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;src must not be None if edited=False&quot;</span><span class="p">)</span>

    <span class="c1"># Photo&#39;s often converts .JPG to .jpeg or .tif to .tiff on import</span>
    <span class="n">dest_ext</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">suffix</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
    <span class="n">actual_ext</span> <span class="o">=</span> <span class="n">actual_suffix</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
    <span class="n">suffixes</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">([</span><span class="n">dest_ext</span><span class="p">,</span> <span class="n">actual_ext</span><span class="p">])</span>
    <span class="k">return</span> <span class="p">(</span>
        <span class="n">dest_ext</span> <span class="o">==</span> <span class="n">actual_ext</span>
        <span class="ow">or</span> <span class="n">suffixes</span> <span class="o">==</span> <span class="p">[</span><span class="s2">&quot;.jpeg&quot;</span><span class="p">,</span> <span class="s2">&quot;.jpg&quot;</span><span class="p">]</span>
        <span class="ow">or</span> <span class="n">suffixes</span> <span class="o">==</span> <span class="p">[</span><span class="s2">&quot;.tif&quot;</span><span class="p">,</span> <span class="s2">&quot;.tiff&quot;</span><span class="p">]</span>
    <span class="p">)</span>


<span class="c1"># not a class method, don&#39;t import into PhotoInfo</span>
<span class="k">def</span> <span class="nf">rename_jpeg_files</span><span class="p">(</span><span class="n">files</span><span class="p">,</span> <span class="n">jpeg_ext</span><span class="p">,</span> <span class="n">fileutil</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;rename any jpeg files in files so that extension matches jpeg_ext</span>

<span class="sd">    Args:</span>
<span class="sd">        files: list of file paths</span>
<span class="sd">        jpeg_ext: extension to use for jpeg files found in files, e.g. &quot;jpg&quot;</span>
<span class="sd">        fileutil: a FileUtil object</span>

<span class="sd">    Returns:</span>
<span class="sd">        list of files with updated names</span>

<span class="sd">    Note: If non-jpeg files found, they will be ignore and returned in the return list</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">jpeg_ext</span> <span class="o">=</span> <span class="s2">&quot;.&quot;</span> <span class="o">+</span> <span class="n">jpeg_ext</span>
    <span class="n">jpegs</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;.jpeg&quot;</span><span class="p">,</span> <span class="s2">&quot;.jpg&quot;</span><span class="p">]</span>
    <span class="n">new_files</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">suffix</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="n">jpegs</span> <span class="ow">and</span> <span class="n">path</span><span class="o">.</span><span class="n">suffix</span> <span class="o">!=</span> <span class="n">jpeg_ext</span><span class="p">:</span>
            <span class="n">new_file</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="p">(</span><span class="n">path</span><span class="o">.</span><span class="n">stem</span> <span class="o">+</span> <span class="n">jpeg_ext</span><span class="p">)</span>
            <span class="n">fileutil</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="n">new_file</span><span class="p">)</span>
            <span class="n">new_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_file</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">new_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">new_files</span>


<span class="k">def</span> <span class="nf">export</span><span class="p">(</span>
    <span class="bp">self</span><span class="p">,</span>
    <span class="n">dest</span><span class="p">,</span>
    <span class="n">filename</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">edited</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">live_photo</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">raw_photo</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">export_as_hardlink</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">overwrite</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">increment</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">sidecar_json</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">sidecar_exiftool</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">sidecar_xmp</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">use_photos_export</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">timeout</span><span class="o">=</span><span class="mi">120</span><span class="p">,</span>
    <span class="n">exiftool</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">keyword_template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">description_template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">render_options</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">RenderOptions</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;export photo</span>
<span class="sd">    dest: must be valid destination path (or exception raised)</span>
<span class="sd">    filename: (optional): name of exported picture; if not provided, will use current filename</span>
<span class="sd">                **NOTE**: if provided, user must ensure file extension (suffix) is correct.</span>
<span class="sd">                For example, if photo is .CR2 file, edited image may be .jpeg.</span>
<span class="sd">                If you provide an extension different than what the actual file is,</span>
<span class="sd">                export will print a warning but will export the photo using the</span>
<span class="sd">                incorrect file extension (unless use_photos_export is true, in which case export will</span>
<span class="sd">                use the extension provided by Photos upon export; in this case, an incorrect extension is</span>
<span class="sd">                silently ignored).</span>
<span class="sd">                e.g. to get the extension of the edited photo,</span>
<span class="sd">                reference PhotoInfo.path_edited</span>
<span class="sd">    edited: (boolean, default=False); if True will export the edited version of the photo, otherwise exports the original version</span>
<span class="sd">            (or raise exception if no edited version)</span>
<span class="sd">    live_photo: (boolean, default=False); if True, will also export the associated .mov for live photos</span>
<span class="sd">    raw_photo: (boolean, default=False); if True, will also export the associated RAW photo</span>
<span class="sd">    export_as_hardlink: (boolean, default=False); if True, will hardlink files instead of copying them</span>
<span class="sd">    overwrite: (boolean, default=False); if True will overwrite files if they already exist</span>
<span class="sd">    increment: (boolean, default=True); if True, will increment file name until a non-existant name is found</span>
<span class="sd">                if overwrite=False and increment=False, export will fail if destination file already exists</span>
<span class="sd">    sidecar_json: if set will write a json sidecar with data in format readable by exiftool</span>
<span class="sd">                sidecar filename will be dest/filename.json; includes exiftool tag group names (e.g. `exiftool -G -j`)</span>
<span class="sd">    sidecar_exiftool: if set will write a json sidecar with data in format readable by exiftool</span>
<span class="sd">                sidecar filename will be dest/filename.json; does not include exiftool tag group names (e.g. `exiftool -j`)</span>
<span class="sd">    sidecar_xmp: if set will write an XMP sidecar with IPTC data</span>
<span class="sd">                sidecar filename will be dest/filename.xmp</span>
<span class="sd">    use_photos_export: (boolean, default=False); if True will attempt to export photo via applescript interaction with Photos</span>
<span class="sd">    timeout: (int, default=120) timeout in seconds used with use_photos_export</span>
<span class="sd">    exiftool: (boolean, default = False); if True, will use exiftool to write metadata to export file</span>
<span class="sd">    returns list of full paths to the exported files</span>
<span class="sd">    use_albums_as_keywords: (boolean, default = False); if True, will include album names in keywords</span>
<span class="sd">    when exporting metadata with exiftool or sidecar</span>
<span class="sd">    use_persons_as_keywords: (boolean, default = False); if True, will include person names in keywords</span>
<span class="sd">    when exporting metadata with exiftool or sidecar</span>
<span class="sd">    keyword_template: (list of strings); list of template strings that will be rendered as used as keywords</span>
<span class="sd">    description_template: string; optional template string that will be rendered for use as photo description</span>
<span class="sd">    render_options: an optional osxphotos.phototemplate.RenderOptions instance with options to pass to template renderer</span>

<span class="sd">    Returns: list of photos exported</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Implementation note: calls export2 to actually do the work</span>

    <span class="n">sidecar</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">if</span> <span class="n">sidecar_json</span><span class="p">:</span>
        <span class="n">sidecar</span> <span class="o">|=</span> <span class="n">SIDECAR_JSON</span>
    <span class="k">if</span> <span class="n">sidecar_exiftool</span><span class="p">:</span>
        <span class="n">sidecar</span> <span class="o">|=</span> <span class="n">SIDECAR_EXIFTOOL</span>
    <span class="k">if</span> <span class="n">sidecar_xmp</span><span class="p">:</span>
        <span class="n">sidecar</span> <span class="o">|=</span> <span class="n">SIDECAR_XMP</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">filename</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">edited</span><span class="p">:</span>
            <span class="n">filename</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_filename</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">original_name</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">original_filename</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">path_edited</span><span class="p">:</span>
                <span class="n">ext</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">path_edited</span><span class="p">)</span><span class="o">.</span><span class="n">suffix</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">uti</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">uti_edited</span> <span class="k">if</span> <span class="n">edited</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">uti_edited</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">uti</span>
                <span class="n">ext</span> <span class="o">=</span> <span class="n">get_preferred_uti_extension</span><span class="p">(</span><span class="n">uti</span><span class="p">)</span>
                <span class="n">ext</span> <span class="o">=</span> <span class="s2">&quot;.&quot;</span> <span class="o">+</span> <span class="n">ext</span>
            <span class="n">filename</span> <span class="o">=</span> <span class="n">original_name</span><span class="o">.</span><span class="n">stem</span> <span class="o">+</span> <span class="s2">&quot;_edited&quot;</span> <span class="o">+</span> <span class="n">ext</span>

    <span class="n">results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">export2</span><span class="p">(</span>
        <span class="n">dest</span><span class="p">,</span>
        <span class="n">original</span><span class="o">=</span><span class="ow">not</span> <span class="n">edited</span><span class="p">,</span>
        <span class="n">original_filename</span><span class="o">=</span><span class="n">filename</span><span class="p">,</span>
        <span class="n">edited</span><span class="o">=</span><span class="n">edited</span><span class="p">,</span>
        <span class="n">edited_filename</span><span class="o">=</span><span class="n">filename</span><span class="p">,</span>
        <span class="n">live_photo</span><span class="o">=</span><span class="n">live_photo</span><span class="p">,</span>
        <span class="n">raw_photo</span><span class="o">=</span><span class="n">raw_photo</span><span class="p">,</span>
        <span class="n">export_as_hardlink</span><span class="o">=</span><span class="n">export_as_hardlink</span><span class="p">,</span>
        <span class="n">overwrite</span><span class="o">=</span><span class="n">overwrite</span><span class="p">,</span>
        <span class="n">increment</span><span class="o">=</span><span class="n">increment</span><span class="p">,</span>
        <span class="n">sidecar</span><span class="o">=</span><span class="n">sidecar</span><span class="p">,</span>
        <span class="n">use_photos_export</span><span class="o">=</span><span class="n">use_photos_export</span><span class="p">,</span>
        <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span><span class="p">,</span>
        <span class="n">exiftool</span><span class="o">=</span><span class="n">exiftool</span><span class="p">,</span>
        <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="n">use_albums_as_keywords</span><span class="p">,</span>
        <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="n">use_persons_as_keywords</span><span class="p">,</span>
        <span class="n">keyword_template</span><span class="o">=</span><span class="n">keyword_template</span><span class="p">,</span>
        <span class="n">description_template</span><span class="o">=</span><span class="n">description_template</span><span class="p">,</span>
        <span class="n">render_options</span><span class="o">=</span><span class="n">render_options</span><span class="p">,</span>
    <span class="p">)</span>

    <span class="k">return</span> <span class="n">results</span><span class="o">.</span><span class="n">exported</span>


<span class="k">def</span> <span class="nf">export2</span><span class="p">(</span>
    <span class="bp">self</span><span class="p">,</span>
    <span class="n">dest</span><span class="p">,</span>
    <span class="n">original</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">original_filename</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">edited</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">edited_filename</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">live_photo</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">raw_photo</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">export_as_hardlink</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">overwrite</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">increment</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">sidecar</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
    <span class="n">sidecar_drop_ext</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">use_photos_export</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">timeout</span><span class="o">=</span><span class="mi">120</span><span class="p">,</span>
    <span class="n">exiftool</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">keyword_template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">description_template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">update</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">ignore_signature</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">export_db</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">fileutil</span><span class="o">=</span><span class="n">FileUtil</span><span class="p">,</span>
    <span class="n">dry_run</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">touch_file</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">convert_to_jpeg</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">jpeg_quality</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span>
    <span class="n">ignore_date_modified</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">use_photokit</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">verbose</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">exiftool_flags</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">merge_exif_persons</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">jpeg_ext</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">persons</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">location</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">replace_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">preview</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">preview_suffix</span><span class="o">=</span><span class="n">DEFAULT_PREVIEW_SUFFIX</span><span class="p">,</span>
    <span class="n">render_options</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">RenderOptions</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
    <span class="n">strip</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;export photo, like export but with update and dry_run options</span>
<span class="sd">    dest: must be valid destination path or exception raised</span>
<span class="sd">    filename: (optional): name of exported picture; if not provided, will use current filename</span>
<span class="sd">                **NOTE**: if provided, user must ensure file extension (suffix) is correct.</span>
<span class="sd">                For example, if photo is .CR2 file, edited image may be .jpeg.</span>
<span class="sd">                If you provide an extension different than what the actual file is,</span>
<span class="sd">                will export the photo using the incorrect file extension (unless use_photos_export is true,</span>
<span class="sd">                in which case export will use the extension provided by Photos upon export.</span>
<span class="sd">                e.g. to get the extension of the edited photo,</span>
<span class="sd">                reference PhotoInfo.path_edited</span>
<span class="sd">    original: (boolean, default=True); if True, will export the original version of the photo</span>
<span class="sd">    edited: (boolean, default=False); if True will export the edited version of the photo</span>
<span class="sd">    live_photo: (boolean, default=False); if True, will also export the associated .mov for live photos</span>
<span class="sd">    raw_photo: (boolean, default=False); if True, will also export the associated RAW photo</span>
<span class="sd">    export_as_hardlink: (boolean, default=False); if True, will hardlink files instead of copying them</span>
<span class="sd">    overwrite: (boolean, default=False); if True will overwrite files if they already exist</span>
<span class="sd">    increment: (boolean, default=True); if True, will increment file name until a non-existant name is found</span>
<span class="sd">                if overwrite=False and increment=False, export will fail if destination file already exists</span>
<span class="sd">    sidecar: bit field: set to one or more of SIDECAR_XMP, SIDECAR_JSON, SIDECAR_EXIFTOOL</span>
<span class="sd">             SIDECAR_JSON: if set will write a json sidecar with data in format readable by exiftool</span>
<span class="sd">                sidecar filename will be dest/filename.json; includes exiftool tag group names (e.g. `exiftool -G -j`)</span>
<span class="sd">             SIDECAR_EXIFTOOL: if set will write a json sidecar with data in format readable by exiftool</span>
<span class="sd">                sidecar filename will be dest/filename.json; does not include exiftool tag group names (e.g. `exiftool -j`)</span>
<span class="sd">             SIDECAR_XMP: if set will write an XMP sidecar with IPTC data</span>
<span class="sd">                sidecar filename will be dest/filename.xmp</span>
<span class="sd">    sidecar_drop_ext: (boolean, default=False); if True, drops the photo&#39;s extension from sidecar filename (e.g. &#39;IMG_1234.json&#39; instead of &#39;IMG_1234.JPG.json&#39;)</span>
<span class="sd">    use_photos_export: (boolean, default=False); if True will attempt to export photo via applescript interaction with Photos</span>
<span class="sd">    timeout: (int, default=120) timeout in seconds used with use_photos_export</span>
<span class="sd">    exiftool: (boolean, default = False); if True, will use exiftool to write metadata to export file</span>
<span class="sd">    use_albums_as_keywords: (boolean, default = False); if True, will include album names in keywords</span>
<span class="sd">    when exporting metadata with exiftool or sidecar</span>
<span class="sd">    use_persons_as_keywords: (boolean, default = False); if True, will include person names in keywords</span>
<span class="sd">    when exporting metadata with exiftool or sidecar</span>
<span class="sd">    keyword_template: (list of strings); list of template strings that will be rendered as used as keywords</span>
<span class="sd">    description_template: string; optional template string that will be rendered for use as photo description</span>
<span class="sd">    update: (boolean, default=False); if True export will run in update mode, that is, it will</span>
<span class="sd">            not export the photo if the current version already exists in the destination</span>
<span class="sd">    ignore_signature: (bool, default=False), ignore file signature when used with update (look only at filename)</span>
<span class="sd">    export_db: (ExportDB_ABC); instance of a class that conforms to ExportDB_ABC with methods</span>
<span class="sd">            for getting/setting data related to exported files to compare update state</span>
<span class="sd">    fileutil: (FileUtilABC); class that conforms to FileUtilABC with various file utilities</span>
<span class="sd">    dry_run: (boolean, default=False); set to True to run in &quot;dry run&quot; mode</span>
<span class="sd">    touch_file: (boolean, default=False); if True, sets file&#39;s modification time upon photo date</span>
<span class="sd">    convert_to_jpeg: boolean; if True, converts non-jpeg images to jpeg</span>
<span class="sd">    jpeg_quality: float in range 0.0 &lt;= jpeg_quality &lt;= 1.0.  A value of 1.0 specifies use best quality, a value of 0.0 specifies use maximum compression.</span>
<span class="sd">    ignore_date_modified: for use with sidecar and exiftool; if True, sets EXIF:ModifyDate to EXIF:DateTimeOriginal even if date_modified is set</span>
<span class="sd">    verbose: optional callable function to use for printing verbose text during processing; if None (default), does not print output.</span>
<span class="sd">    exiftool_flags: optional list of flags to pass to exiftool when using exiftool option, e.g [&quot;-m&quot;, &quot;-F&quot;]</span>
<span class="sd">    merge_exif_keywords: boolean; if True, merged keywords found in file&#39;s exif data (requires exiftool)</span>
<span class="sd">    merge_exif_persons: boolean; if True, merged persons found in file&#39;s exif data (requires exiftool)</span>
<span class="sd">    jpeg_ext: if set, will use this value for extension on jpegs converted to jpeg with convert_to_jpeg; if not set, uses jpeg; do not include the leading &quot;.&quot;</span>
<span class="sd">    persons: if True, include persons in exported metadata</span>
<span class="sd">    location: if True, include location in exported metadata</span>
<span class="sd">    replace_keywords: if True, keyword_template replaces any keywords, otherwise it&#39;s additive</span>
<span class="sd">    preview: if True, also exports preview image</span>
<span class="sd">    preview_suffix: optional string to append to end of filename for preview images</span>
<span class="sd">    render_options: optional osxphotos.phototemplate.RenderOptions instance to specify options for rendering templates</span>
<span class="sd">    strip: if True, strip whitespace from rendered templates</span>

<span class="sd">    Returns: ExportResults class</span>
<span class="sd">        ExportResults has attributes:</span>
<span class="sd">        &quot;exported&quot;,</span>
<span class="sd">        &quot;new&quot;,</span>
<span class="sd">        &quot;updated&quot;,</span>
<span class="sd">        &quot;skipped&quot;,</span>
<span class="sd">        &quot;exif_updated&quot;,</span>
<span class="sd">        &quot;touched&quot;,</span>
<span class="sd">        &quot;converted_to_jpeg&quot;,</span>
<span class="sd">        &quot;sidecar_json_written&quot;,</span>
<span class="sd">        &quot;sidecar_json_skipped&quot;,</span>
<span class="sd">        &quot;sidecar_exiftool_written&quot;,</span>
<span class="sd">        &quot;sidecar_exiftool_skipped&quot;,</span>
<span class="sd">        &quot;sidecar_xmp_written&quot;,</span>
<span class="sd">        &quot;sidecar_xmp_skipped&quot;,</span>
<span class="sd">        &quot;missing&quot;,</span>
<span class="sd">        &quot;error&quot;,</span>
<span class="sd">        &quot;error_str&quot;,</span>
<span class="sd">        &quot;exiftool_warning&quot;,</span>
<span class="sd">        &quot;exiftool_error&quot;,</span>


<span class="sd">    Note: to use dry run mode, you must set dry_run=True and also pass in memory version of export_db,</span>
<span class="sd">          and no-op fileutil (e.g. ExportDBInMemory and FileUtilNoOp)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># NOTE: This function is very complex and does a lot of things.</span>
    <span class="c1"># Don&#39;t modify this code if you don&#39;t fully understand everything it does.</span>
    <span class="c1"># TODO: This is a good candidate for refactoring.</span>

    <span class="c1"># when called from export(), won&#39;t get an export_db, so use no-op version</span>
    <span class="k">if</span> <span class="n">export_db</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">export_db</span> <span class="o">=</span> <span class="n">ExportDBNoOp</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">verbose</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">callable</span><span class="p">(</span><span class="n">verbose</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;verbose must be callable&quot;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">verbose</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">verbose</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_verbose</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">_render_options</span> <span class="o">=</span> <span class="n">render_options</span> <span class="ow">or</span> <span class="n">RenderOptions</span><span class="p">()</span>

    <span class="n">export_original</span> <span class="o">=</span> <span class="n">original</span>
    <span class="n">export_edited</span> <span class="o">=</span> <span class="n">edited</span>
    <span class="k">if</span> <span class="n">edited</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">hasadjustments</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s2">&quot;Photo does not have adjustments, cannot export edited version&quot;</span>
        <span class="p">)</span>

    <span class="c1"># verify destination is a valid path</span>
    <span class="k">if</span> <span class="n">dest</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;dest must not be None&quot;</span><span class="p">)</span>
    <span class="k">elif</span> <span class="ow">not</span> <span class="n">dry_run</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">dest</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">FileNotFoundError</span><span class="p">(</span><span class="s2">&quot;Invalid path passed to export&quot;</span><span class="p">)</span>

    <span class="n">original_filename</span> <span class="o">=</span> <span class="n">original_filename</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_filename</span>
    <span class="n">dest_original</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span> <span class="o">/</span> <span class="n">original_filename</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">edited_filename</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">edited</span><span class="p">:</span>
            <span class="n">edited_filename</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_filename</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">original_name</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">original_filename</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">path_edited</span><span class="p">:</span>
                <span class="n">ext</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">path_edited</span><span class="p">)</span><span class="o">.</span><span class="n">suffix</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">uti</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">uti_edited</span> <span class="k">if</span> <span class="n">edited</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">uti_edited</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">uti</span>
                <span class="n">ext</span> <span class="o">=</span> <span class="n">get_preferred_uti_extension</span><span class="p">(</span><span class="n">uti</span><span class="p">)</span>
                <span class="n">ext</span> <span class="o">=</span> <span class="s2">&quot;.&quot;</span> <span class="o">+</span> <span class="n">ext</span>
            <span class="n">edited_filename</span> <span class="o">=</span> <span class="n">original_name</span><span class="o">.</span><span class="n">stem</span> <span class="o">+</span> <span class="s2">&quot;_edited&quot;</span> <span class="o">+</span> <span class="n">ext</span>
    <span class="n">dest_edited</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span> <span class="o">/</span> <span class="n">edited_filename</span>

    <span class="k">if</span> <span class="n">convert_to_jpeg</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">isphoto</span><span class="p">:</span>
        <span class="n">something_to_convert</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">ext</span> <span class="o">=</span> <span class="s2">&quot;.&quot;</span> <span class="o">+</span> <span class="n">jpeg_ext</span> <span class="k">if</span> <span class="n">jpeg_ext</span> <span class="k">else</span> <span class="s2">&quot;.jpeg&quot;</span>
        <span class="k">if</span> <span class="n">export_original</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">uti_original</span> <span class="o">!=</span> <span class="s2">&quot;public.jpeg&quot;</span><span class="p">:</span>
            <span class="c1"># not a jpeg but will convert to jpeg upon export so fix file extension</span>
            <span class="n">something_to_convert</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">dest_original</span> <span class="o">=</span> <span class="n">dest_original</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest_original</span><span class="o">.</span><span class="n">stem</span><span class="si">}{</span><span class="n">ext</span><span class="si">}</span><span class="s2">&quot;</span>
        <span class="k">if</span> <span class="n">export_edited</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">uti</span> <span class="o">!=</span> <span class="s2">&quot;public.jpeg&quot;</span><span class="p">:</span>
            <span class="c1"># in Big Sur+, edited HEICs are HEIC</span>
            <span class="n">something_to_convert</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">dest_edited</span> <span class="o">=</span> <span class="n">dest_edited</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest_edited</span><span class="o">.</span><span class="n">stem</span><span class="si">}{</span><span class="n">ext</span><span class="si">}</span><span class="s2">&quot;</span>
        <span class="n">convert_to_jpeg</span> <span class="o">=</span> <span class="n">something_to_convert</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">convert_to_jpeg</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="c1"># check to see if file exists and if so, add (1), (2), etc until we find one that works</span>
    <span class="c1"># Photos checks the stem and adds (1), (2), etc which avoids collision with sidecars</span>
    <span class="c1"># e.g. exporting sidecar for file1.png and file1.jpeg</span>
    <span class="c1"># if file1.png exists and exporting file1.jpeg,</span>
    <span class="c1"># dest will be file1 (1).jpeg even though file1.jpeg doesn&#39;t exist to prevent sidecar collision</span>
    <span class="n">increment_file_count</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">if</span> <span class="n">increment</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">update</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">overwrite</span><span class="p">:</span>
        <span class="n">dest_original</span><span class="p">,</span> <span class="n">increment_file_count</span> <span class="o">=</span> <span class="n">increment_filename_with_count</span><span class="p">(</span>
            <span class="n">dest_original</span>
        <span class="p">)</span>
        <span class="n">dest_original</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">dest_original</span><span class="p">)</span>

    <span class="c1"># if overwrite==False and #increment==False, export should fail if file exists</span>
    <span class="k">if</span> <span class="p">(</span>
        <span class="n">dest_original</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span>
        <span class="ow">and</span> <span class="n">export_original</span>
        <span class="ow">and</span> <span class="ow">not</span> <span class="n">update</span>
        <span class="ow">and</span> <span class="ow">not</span> <span class="n">overwrite</span>
        <span class="ow">and</span> <span class="ow">not</span> <span class="n">increment</span>
    <span class="p">):</span>
        <span class="k">raise</span> <span class="ne">FileExistsError</span><span class="p">(</span>
            <span class="sa">f</span><span class="s2">&quot;destination exists (</span><span class="si">{</span><span class="n">dest_original</span><span class="si">}</span><span class="s2">); overwrite=</span><span class="si">{</span><span class="n">overwrite</span><span class="si">}</span><span class="s2">, increment=</span><span class="si">{</span><span class="n">increment</span><span class="si">}</span><span class="s2">&quot;</span>
        <span class="p">)</span>

    <span class="k">if</span> <span class="n">export_edited</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">increment</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">update</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">overwrite</span><span class="p">:</span>
            <span class="n">dest_edited</span><span class="p">,</span> <span class="n">increment_file_count</span> <span class="o">=</span> <span class="n">increment_filename_with_count</span><span class="p">(</span>
                <span class="n">dest_edited</span><span class="p">,</span> <span class="n">increment_file_count</span>
            <span class="p">)</span>
            <span class="n">dest_edited</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">dest_edited</span><span class="p">)</span>

        <span class="c1"># if overwrite==False and #increment==False, export should fail if file exists</span>
        <span class="k">if</span> <span class="n">dest_edited</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">update</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">overwrite</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">increment</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">FileExistsError</span><span class="p">(</span>
                <span class="sa">f</span><span class="s2">&quot;destination exists (</span><span class="si">{</span><span class="n">dest_edited</span><span class="si">}</span><span class="s2">); overwrite=</span><span class="si">{</span><span class="n">overwrite</span><span class="si">}</span><span class="s2">, increment=</span><span class="si">{</span><span class="n">increment</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">_render_options</span><span class="o">.</span><span class="n">filepath</span> <span class="o">=</span> <span class="p">(</span>
        <span class="nb">str</span><span class="p">(</span><span class="n">dest_original</span><span class="p">)</span> <span class="k">if</span> <span class="n">export_original</span> <span class="k">else</span> <span class="nb">str</span><span class="p">(</span><span class="n">dest_edited</span><span class="p">)</span>
    <span class="p">)</span>
    <span class="n">all_results</span> <span class="o">=</span> <span class="n">ExportResults</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">use_photos_export</span><span class="p">:</span>
        <span class="c1"># TODO: collapse these into a single call (refactor _export_photo_with_photos_export)</span>
        <span class="k">if</span> <span class="n">original</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_export_photo_with_photos_export</span><span class="p">(</span>
                <span class="n">dest_original</span><span class="p">,</span>
                <span class="n">all_results</span><span class="p">,</span>
                <span class="n">fileutil</span><span class="p">,</span>
                <span class="n">export_db</span><span class="p">,</span>
                <span class="n">use_photokit</span><span class="o">=</span><span class="n">use_photokit</span><span class="p">,</span>
                <span class="n">dry_run</span><span class="o">=</span><span class="n">dry_run</span><span class="p">,</span>
                <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span><span class="p">,</span>
                <span class="n">jpeg_ext</span><span class="o">=</span><span class="n">jpeg_ext</span><span class="p">,</span>
                <span class="n">touch_file</span><span class="o">=</span><span class="n">touch_file</span><span class="p">,</span>
                <span class="n">update</span><span class="o">=</span><span class="n">update</span><span class="p">,</span>
                <span class="n">overwrite</span><span class="o">=</span><span class="n">overwrite</span><span class="p">,</span>
                <span class="n">live_photo</span><span class="o">=</span><span class="n">live_photo</span><span class="p">,</span>
                <span class="n">edited</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                <span class="n">convert_to_jpeg</span><span class="o">=</span><span class="n">convert_to_jpeg</span><span class="p">,</span>
                <span class="n">jpeg_quality</span><span class="o">=</span><span class="n">jpeg_quality</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">edited</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_export_photo_with_photos_export</span><span class="p">(</span>
                <span class="n">dest_edited</span><span class="p">,</span>
                <span class="n">all_results</span><span class="p">,</span>
                <span class="n">fileutil</span><span class="p">,</span>
                <span class="n">export_db</span><span class="p">,</span>
                <span class="n">use_photokit</span><span class="o">=</span><span class="n">use_photokit</span><span class="p">,</span>
                <span class="n">dry_run</span><span class="o">=</span><span class="n">dry_run</span><span class="p">,</span>
                <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span><span class="p">,</span>
                <span class="n">jpeg_ext</span><span class="o">=</span><span class="n">jpeg_ext</span><span class="p">,</span>
                <span class="n">touch_file</span><span class="o">=</span><span class="n">touch_file</span><span class="p">,</span>
                <span class="n">update</span><span class="o">=</span><span class="n">update</span><span class="p">,</span>
                <span class="n">overwrite</span><span class="o">=</span><span class="n">overwrite</span><span class="p">,</span>
                <span class="n">live_photo</span><span class="o">=</span><span class="n">live_photo</span><span class="p">,</span>
                <span class="n">edited</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                <span class="n">convert_to_jpeg</span><span class="o">=</span><span class="n">convert_to_jpeg</span><span class="p">,</span>
                <span class="n">jpeg_quality</span><span class="o">=</span><span class="n">jpeg_quality</span><span class="p">,</span>
            <span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># find the source file on disk and export</span>
        <span class="c1"># get path to source file and verify it&#39;s not None and is valid file</span>
        <span class="c1"># TODO: how to handle ismissing or not hasadjustments and edited=True cases?</span>
        <span class="n">export_src_dest</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">edited</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">path_edited</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">export_src_dest</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">path_edited</span><span class="p">,</span> <span class="n">dest_edited</span><span class="p">))</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="n">edited</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">path</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">export_src_dest</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">path</span><span class="p">,</span> <span class="n">dest_original</span><span class="p">))</span>

        <span class="k">for</span> <span class="n">src</span><span class="p">,</span> <span class="n">dest</span> <span class="ow">in</span> <span class="n">export_src_dest</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">src</span><span class="p">)</span><span class="o">.</span><span class="n">is_file</span><span class="p">():</span>
                <span class="k">raise</span> <span class="ne">FileNotFoundError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">src</span><span class="si">}</span><span class="s2"> does not appear to exist&quot;</span><span class="p">)</span>

            <span class="c1"># found source now try to find right destination</span>
            <span class="k">if</span> <span class="n">update</span> <span class="ow">and</span> <span class="n">dest</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
                <span class="c1"># destination exists, check to see if destination is the right UUID</span>
                <span class="n">dest_uuid</span> <span class="o">=</span> <span class="n">export_db</span><span class="o">.</span><span class="n">get_uuid_for_file</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">dest_uuid</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">):</span>
                    <span class="c1"># might be exporting into a pre-ExportDB folder or the DB got deleted</span>
                    <span class="n">dest_uuid</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">uuid</span>
                    <span class="n">export_db</span><span class="o">.</span><span class="n">set_data</span><span class="p">(</span>
                        <span class="n">filename</span><span class="o">=</span><span class="n">dest</span><span class="p">,</span>
                        <span class="n">uuid</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">uuid</span><span class="p">,</span>
                        <span class="n">orig_stat</span><span class="o">=</span><span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span>
                        <span class="n">exif_stat</span><span class="o">=</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
                        <span class="n">converted_stat</span><span class="o">=</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
                        <span class="n">edited_stat</span><span class="o">=</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
                        <span class="n">info_json</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">json</span><span class="p">(),</span>
                        <span class="n">exif_json</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="k">if</span> <span class="n">dest_uuid</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">uuid</span><span class="p">:</span>
                    <span class="c1"># not the right file, find the right one</span>
                    <span class="n">glob_str</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}</span><span class="s2"> (*</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">suffix</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
                    <span class="n">dest_files</span> <span class="o">=</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="n">glob_str</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">file_</span> <span class="ow">in</span> <span class="n">dest_files</span><span class="p">:</span>
                        <span class="n">dest_uuid</span> <span class="o">=</span> <span class="n">export_db</span><span class="o">.</span><span class="n">get_uuid_for_file</span><span class="p">(</span><span class="n">file_</span><span class="p">)</span>
                        <span class="k">if</span> <span class="n">dest_uuid</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">uuid</span><span class="p">:</span>
                            <span class="n">dest</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">file_</span><span class="p">)</span>
                            <span class="k">break</span>
                        <span class="k">elif</span> <span class="n">dest_uuid</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">file_</span><span class="p">):</span>
                            <span class="c1"># files match, update the UUID</span>
                            <span class="n">dest</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">file_</span><span class="p">)</span>
                            <span class="n">export_db</span><span class="o">.</span><span class="n">set_data</span><span class="p">(</span>
                                <span class="n">filename</span><span class="o">=</span><span class="n">dest</span><span class="p">,</span>
                                <span class="n">uuid</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">uuid</span><span class="p">,</span>
                                <span class="n">orig_stat</span><span class="o">=</span><span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span>
                                <span class="n">exif_stat</span><span class="o">=</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
                                <span class="n">converted_stat</span><span class="o">=</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
                                <span class="n">edited_stat</span><span class="o">=</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
                                <span class="n">info_json</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">json</span><span class="p">(),</span>
                                <span class="n">exif_json</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                            <span class="p">)</span>
                            <span class="k">break</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="c1"># increment the destination file</span>
                        <span class="n">dest</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">increment_filename</span><span class="p">(</span><span class="n">dest</span><span class="p">))</span>

            <span class="k">if</span> <span class="n">export_original</span><span class="p">:</span>
                <span class="n">dest_original</span> <span class="o">=</span> <span class="n">dest</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">dest_edited</span> <span class="o">=</span> <span class="n">dest</span>

            <span class="c1"># export the dest file</span>
            <span class="n">results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_export_photo</span><span class="p">(</span>
                <span class="n">src</span><span class="p">,</span>
                <span class="n">dest</span><span class="p">,</span>
                <span class="n">update</span><span class="p">,</span>
                <span class="n">export_db</span><span class="p">,</span>
                <span class="n">overwrite</span><span class="p">,</span>
                <span class="n">export_as_hardlink</span><span class="p">,</span>
                <span class="n">exiftool</span><span class="p">,</span>
                <span class="n">touch_file</span><span class="p">,</span>
                <span class="n">convert_to_jpeg</span><span class="p">,</span>
                <span class="n">fileutil</span><span class="o">=</span><span class="n">fileutil</span><span class="p">,</span>
                <span class="n">edited</span><span class="o">=</span><span class="n">edited</span><span class="p">,</span>
                <span class="n">jpeg_quality</span><span class="o">=</span><span class="n">jpeg_quality</span><span class="p">,</span>
                <span class="n">ignore_signature</span><span class="o">=</span><span class="n">ignore_signature</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="n">all_results</span> <span class="o">+=</span> <span class="n">results</span>

        <span class="n">dest</span> <span class="o">=</span> <span class="n">dest_original</span> <span class="k">if</span> <span class="n">export_original</span> <span class="k">else</span> <span class="n">dest_edited</span>

        <span class="c1"># copy live photo associated .mov if requested</span>
        <span class="k">if</span> <span class="n">export_original</span> <span class="ow">and</span> <span class="n">live_photo</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">live_photo</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">path_live_photo</span><span class="p">:</span>
            <span class="n">live_name</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}</span><span class="s2">.mov&quot;</span>
            <span class="n">src_live</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">path_live_photo</span>
            <span class="n">results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_export_photo</span><span class="p">(</span>
                <span class="n">src_live</span><span class="p">,</span>
                <span class="n">live_name</span><span class="p">,</span>
                <span class="n">update</span><span class="p">,</span>
                <span class="n">export_db</span><span class="p">,</span>
                <span class="n">overwrite</span><span class="p">,</span>
                <span class="n">export_as_hardlink</span><span class="p">,</span>
                <span class="n">exiftool</span><span class="p">,</span>
                <span class="n">touch_file</span><span class="p">,</span>
                <span class="kc">False</span><span class="p">,</span>
                <span class="n">fileutil</span><span class="o">=</span><span class="n">fileutil</span><span class="p">,</span>
                <span class="n">ignore_signature</span><span class="o">=</span><span class="n">ignore_signature</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="n">all_results</span> <span class="o">+=</span> <span class="n">results</span>

        <span class="k">if</span> <span class="p">(</span>
            <span class="n">export_edited</span>
            <span class="ow">and</span> <span class="n">live_photo</span>
            <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">live_photo</span>
            <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">path_edited_live_photo</span>
        <span class="p">):</span>
            <span class="n">live_name</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest_edited</span><span class="o">.</span><span class="n">stem</span><span class="si">}</span><span class="s2">.mov&quot;</span>
            <span class="n">src_live</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">path_edited_live_photo</span>
            <span class="n">results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_export_photo</span><span class="p">(</span>
                <span class="n">src_live</span><span class="p">,</span>
                <span class="n">live_name</span><span class="p">,</span>
                <span class="n">update</span><span class="p">,</span>
                <span class="n">export_db</span><span class="p">,</span>
                <span class="n">overwrite</span><span class="p">,</span>
                <span class="n">export_as_hardlink</span><span class="p">,</span>
                <span class="n">exiftool</span><span class="p">,</span>
                <span class="n">touch_file</span><span class="p">,</span>
                <span class="kc">False</span><span class="p">,</span>
                <span class="n">fileutil</span><span class="o">=</span><span class="n">fileutil</span><span class="p">,</span>
                <span class="n">ignore_signature</span><span class="o">=</span><span class="n">ignore_signature</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="n">all_results</span> <span class="o">+=</span> <span class="n">results</span>

        <span class="c1"># copy associated RAW image if requested</span>
        <span class="k">if</span> <span class="n">raw_photo</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">has_raw</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">path_raw</span><span class="p">:</span>
            <span class="n">raw_path</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">path_raw</span><span class="p">)</span>
            <span class="n">raw_ext</span> <span class="o">=</span> <span class="n">raw_path</span><span class="o">.</span><span class="n">suffix</span>
            <span class="n">raw_name</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}{</span><span class="n">raw_ext</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="k">if</span> <span class="n">raw_path</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_export_photo</span><span class="p">(</span>
                    <span class="n">raw_path</span><span class="p">,</span>
                    <span class="n">raw_name</span><span class="p">,</span>
                    <span class="n">update</span><span class="p">,</span>
                    <span class="n">export_db</span><span class="p">,</span>
                    <span class="n">overwrite</span><span class="p">,</span>
                    <span class="n">export_as_hardlink</span><span class="p">,</span>
                    <span class="n">exiftool</span><span class="p">,</span>
                    <span class="n">touch_file</span><span class="p">,</span>
                    <span class="n">convert_to_jpeg</span><span class="p">,</span>
                    <span class="n">fileutil</span><span class="o">=</span><span class="n">fileutil</span><span class="p">,</span>
                    <span class="n">jpeg_quality</span><span class="o">=</span><span class="n">jpeg_quality</span><span class="p">,</span>
                    <span class="n">ignore_signature</span><span class="o">=</span><span class="n">ignore_signature</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="n">all_results</span> <span class="o">+=</span> <span class="n">results</span>

        <span class="c1"># copy preview image if requested</span>
        <span class="k">if</span> <span class="n">preview</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">path_derivatives</span><span class="p">:</span>
            <span class="c1"># Photos keeps multiple different derivatives and path_derivatives returns list of them</span>
            <span class="c1"># first derivative is the largest so export that one</span>
            <span class="n">preview_path</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">path_derivatives</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="n">preview_ext</span> <span class="o">=</span> <span class="n">preview_path</span><span class="o">.</span><span class="n">suffix</span>
            <span class="n">preview_name</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}{</span><span class="n">preview_suffix</span><span class="si">}{</span><span class="n">preview_ext</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="c1"># if original is missing, the filename won&#39;t have been incremented so</span>
            <span class="c1"># need to check here to make sure there aren&#39;t duplicate preview files in</span>
            <span class="c1"># the export directory</span>
            <span class="n">preview_name</span> <span class="o">=</span> <span class="p">(</span>
                <span class="n">preview_name</span>
                <span class="k">if</span> <span class="n">overwrite</span> <span class="ow">or</span> <span class="n">update</span>
                <span class="k">else</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">increment_filename</span><span class="p">(</span><span class="n">preview_name</span><span class="p">))</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="n">preview_path</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_export_photo</span><span class="p">(</span>
                    <span class="n">preview_path</span><span class="p">,</span>
                    <span class="n">preview_name</span><span class="p">,</span>
                    <span class="n">update</span><span class="p">,</span>
                    <span class="n">export_db</span><span class="p">,</span>
                    <span class="n">overwrite</span><span class="p">,</span>
                    <span class="n">export_as_hardlink</span><span class="p">,</span>
                    <span class="n">exiftool</span><span class="p">,</span>
                    <span class="n">touch_file</span><span class="p">,</span>
                    <span class="n">convert_to_jpeg</span><span class="p">,</span>
                    <span class="n">fileutil</span><span class="o">=</span><span class="n">fileutil</span><span class="p">,</span>
                    <span class="n">jpeg_quality</span><span class="o">=</span><span class="n">jpeg_quality</span><span class="p">,</span>
                    <span class="n">ignore_signature</span><span class="o">=</span><span class="n">ignore_signature</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="n">all_results</span> <span class="o">+=</span> <span class="n">results</span>

    <span class="c1"># export metadata</span>
    <span class="n">sidecars</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">sidecar_json_files_skipped</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">sidecar_json_files_written</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">sidecar_exiftool_files_skipped</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">sidecar_exiftool_files_written</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">sidecar_xmp_files_skipped</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">sidecar_xmp_files_written</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="n">dest</span> <span class="o">=</span> <span class="n">dest_original</span> <span class="k">if</span> <span class="n">export_original</span> <span class="k">else</span> <span class="n">dest_edited</span>
    <span class="n">dest_suffix</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span> <span class="k">if</span> <span class="n">sidecar_drop_ext</span> <span class="k">else</span> <span class="n">dest</span><span class="o">.</span><span class="n">suffix</span>
    <span class="k">if</span> <span class="n">sidecar</span> <span class="o">&amp;</span> <span class="n">SIDECAR_JSON</span><span class="p">:</span>
        <span class="n">sidecar_filename</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}{</span><span class="n">dest_suffix</span><span class="si">}</span><span class="s2">.json&quot;</span><span class="p">)</span>
        <span class="n">sidecar_str</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_exiftool_json_sidecar</span><span class="p">(</span>
            <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="n">use_albums_as_keywords</span><span class="p">,</span>
            <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="n">use_persons_as_keywords</span><span class="p">,</span>
            <span class="n">keyword_template</span><span class="o">=</span><span class="n">keyword_template</span><span class="p">,</span>
            <span class="n">description_template</span><span class="o">=</span><span class="n">description_template</span><span class="p">,</span>
            <span class="n">ignore_date_modified</span><span class="o">=</span><span class="n">ignore_date_modified</span><span class="p">,</span>
            <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="n">merge_exif_keywords</span><span class="p">,</span>
            <span class="n">merge_exif_persons</span><span class="o">=</span><span class="n">merge_exif_persons</span><span class="p">,</span>
            <span class="n">filename</span><span class="o">=</span><span class="n">dest</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="n">persons</span><span class="o">=</span><span class="n">persons</span><span class="p">,</span>
            <span class="n">location</span><span class="o">=</span><span class="n">location</span><span class="p">,</span>
            <span class="n">replace_keywords</span><span class="o">=</span><span class="n">replace_keywords</span><span class="p">,</span>
            <span class="n">strip</span><span class="o">=</span><span class="n">strip</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">sidecars</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="p">(</span>
                <span class="n">sidecar_filename</span><span class="p">,</span>
                <span class="n">sidecar_str</span><span class="p">,</span>
                <span class="n">sidecar_json_files_written</span><span class="p">,</span>
                <span class="n">sidecar_json_files_skipped</span><span class="p">,</span>
                <span class="s2">&quot;JSON&quot;</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="p">)</span>

    <span class="k">if</span> <span class="n">sidecar</span> <span class="o">&amp;</span> <span class="n">SIDECAR_EXIFTOOL</span><span class="p">:</span>
        <span class="n">sidecar_filename</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}{</span><span class="n">dest_suffix</span><span class="si">}</span><span class="s2">.json&quot;</span><span class="p">)</span>
        <span class="n">sidecar_str</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_exiftool_json_sidecar</span><span class="p">(</span>
            <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="n">use_albums_as_keywords</span><span class="p">,</span>
            <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="n">use_persons_as_keywords</span><span class="p">,</span>
            <span class="n">keyword_template</span><span class="o">=</span><span class="n">keyword_template</span><span class="p">,</span>
            <span class="n">description_template</span><span class="o">=</span><span class="n">description_template</span><span class="p">,</span>
            <span class="n">ignore_date_modified</span><span class="o">=</span><span class="n">ignore_date_modified</span><span class="p">,</span>
            <span class="n">tag_groups</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
            <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="n">merge_exif_keywords</span><span class="p">,</span>
            <span class="n">merge_exif_persons</span><span class="o">=</span><span class="n">merge_exif_persons</span><span class="p">,</span>
            <span class="n">filename</span><span class="o">=</span><span class="n">dest</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="n">persons</span><span class="o">=</span><span class="n">persons</span><span class="p">,</span>
            <span class="n">location</span><span class="o">=</span><span class="n">location</span><span class="p">,</span>
            <span class="n">replace_keywords</span><span class="o">=</span><span class="n">replace_keywords</span><span class="p">,</span>
            <span class="n">strip</span><span class="o">=</span><span class="n">strip</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">sidecars</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="p">(</span>
                <span class="n">sidecar_filename</span><span class="p">,</span>
                <span class="n">sidecar_str</span><span class="p">,</span>
                <span class="n">sidecar_exiftool_files_written</span><span class="p">,</span>
                <span class="n">sidecar_exiftool_files_skipped</span><span class="p">,</span>
                <span class="s2">&quot;exiftool&quot;</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="p">)</span>

    <span class="k">if</span> <span class="n">sidecar</span> <span class="o">&amp;</span> <span class="n">SIDECAR_XMP</span><span class="p">:</span>
        <span class="n">sidecar_filename</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}{</span><span class="n">dest_suffix</span><span class="si">}</span><span class="s2">.xmp&quot;</span><span class="p">)</span>
        <span class="n">sidecar_str</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_xmp_sidecar</span><span class="p">(</span>
            <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="n">use_albums_as_keywords</span><span class="p">,</span>
            <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="n">use_persons_as_keywords</span><span class="p">,</span>
            <span class="n">keyword_template</span><span class="o">=</span><span class="n">keyword_template</span><span class="p">,</span>
            <span class="n">description_template</span><span class="o">=</span><span class="n">description_template</span><span class="p">,</span>
            <span class="n">extension</span><span class="o">=</span><span class="n">dest</span><span class="o">.</span><span class="n">suffix</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="k">if</span> <span class="n">dest</span><span class="o">.</span><span class="n">suffix</span> <span class="k">else</span> <span class="kc">None</span><span class="p">,</span>
            <span class="n">persons</span><span class="o">=</span><span class="n">persons</span><span class="p">,</span>
            <span class="n">location</span><span class="o">=</span><span class="n">location</span><span class="p">,</span>
            <span class="n">replace_keywords</span><span class="o">=</span><span class="n">replace_keywords</span><span class="p">,</span>
            <span class="n">strip</span><span class="o">=</span><span class="n">strip</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">sidecars</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="p">(</span>
                <span class="n">sidecar_filename</span><span class="p">,</span>
                <span class="n">sidecar_str</span><span class="p">,</span>
                <span class="n">sidecar_xmp_files_written</span><span class="p">,</span>
                <span class="n">sidecar_xmp_files_skipped</span><span class="p">,</span>
                <span class="s2">&quot;XMP&quot;</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="p">)</span>

    <span class="k">for</span> <span class="n">data</span> <span class="ow">in</span> <span class="n">sidecars</span><span class="p">:</span>
        <span class="n">sidecar_filename</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">sidecar_str</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">files_written</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
        <span class="n">files_skipped</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
        <span class="n">sidecar_type</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>

        <span class="n">sidecar_digest</span> <span class="o">=</span> <span class="n">hexdigest</span><span class="p">(</span><span class="n">sidecar_str</span><span class="p">)</span>
        <span class="n">old_sidecar_digest</span><span class="p">,</span> <span class="n">sidecar_sig</span> <span class="o">=</span> <span class="n">export_db</span><span class="o">.</span><span class="n">get_sidecar_for_file</span><span class="p">(</span>
            <span class="n">sidecar_filename</span>
        <span class="p">)</span>
        <span class="n">write_sidecar</span> <span class="o">=</span> <span class="p">(</span>
            <span class="ow">not</span> <span class="n">update</span>
            <span class="ow">or</span> <span class="p">(</span><span class="n">update</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">sidecar_filename</span><span class="o">.</span><span class="n">exists</span><span class="p">())</span>
            <span class="ow">or</span> <span class="p">(</span>
                <span class="n">update</span>
                <span class="ow">and</span> <span class="p">(</span><span class="n">sidecar_digest</span> <span class="o">!=</span> <span class="n">old_sidecar_digest</span><span class="p">)</span>
                <span class="ow">or</span> <span class="ow">not</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp_file_sig</span><span class="p">(</span><span class="n">sidecar_filename</span><span class="p">,</span> <span class="n">sidecar_sig</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="n">write_sidecar</span><span class="p">:</span>
            <span class="n">verbose</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Writing </span><span class="si">{</span><span class="n">sidecar_type</span><span class="si">}</span><span class="s2"> sidecar </span><span class="si">{</span><span class="n">sidecar_filename</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="n">files_written</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">sidecar_filename</span><span class="p">))</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">dry_run</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_write_sidecar</span><span class="p">(</span><span class="n">sidecar_filename</span><span class="p">,</span> <span class="n">sidecar_str</span><span class="p">)</span>
                <span class="n">export_db</span><span class="o">.</span><span class="n">set_sidecar_for_file</span><span class="p">(</span>
                    <span class="n">sidecar_filename</span><span class="p">,</span>
                    <span class="n">sidecar_digest</span><span class="p">,</span>
                    <span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">sidecar_filename</span><span class="p">),</span>
                <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">verbose</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Skipped up to date </span><span class="si">{</span><span class="n">sidecar_type</span><span class="si">}</span><span class="s2"> sidecar </span><span class="si">{</span><span class="n">sidecar_filename</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="n">files_skipped</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">sidecar_filename</span><span class="p">))</span>

    <span class="c1"># if exiftool, write the metadata</span>
    <span class="k">if</span> <span class="n">update</span><span class="p">:</span>
        <span class="n">exif_files</span> <span class="o">=</span> <span class="n">all_results</span><span class="o">.</span><span class="n">new</span> <span class="o">+</span> <span class="n">all_results</span><span class="o">.</span><span class="n">updated</span> <span class="o">+</span> <span class="n">all_results</span><span class="o">.</span><span class="n">skipped</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">exif_files</span> <span class="o">=</span> <span class="n">all_results</span><span class="o">.</span><span class="n">exported</span>

    <span class="c1"># TODO: remove duplicative code from below</span>
    <span class="k">if</span> <span class="n">exiftool</span> <span class="ow">and</span> <span class="n">update</span> <span class="ow">and</span> <span class="n">exif_files</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">exported_file</span> <span class="ow">in</span> <span class="n">exif_files</span><span class="p">:</span>
            <span class="n">files_are_different</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="n">old_data</span> <span class="o">=</span> <span class="n">export_db</span><span class="o">.</span><span class="n">get_exifdata_for_file</span><span class="p">(</span><span class="n">exported_file</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">old_data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">old_data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">old_data</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">current_data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_exiftool_json_sidecar</span><span class="p">(</span>
                        <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="n">use_albums_as_keywords</span><span class="p">,</span>
                        <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="n">use_persons_as_keywords</span><span class="p">,</span>
                        <span class="n">keyword_template</span><span class="o">=</span><span class="n">keyword_template</span><span class="p">,</span>
                        <span class="n">description_template</span><span class="o">=</span><span class="n">description_template</span><span class="p">,</span>
                        <span class="n">ignore_date_modified</span><span class="o">=</span><span class="n">ignore_date_modified</span><span class="p">,</span>
                        <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="n">merge_exif_keywords</span><span class="p">,</span>
                        <span class="n">merge_exif_persons</span><span class="o">=</span><span class="n">merge_exif_persons</span><span class="p">,</span>
                        <span class="n">persons</span><span class="o">=</span><span class="n">persons</span><span class="p">,</span>
                        <span class="n">location</span><span class="o">=</span><span class="n">location</span><span class="p">,</span>
                        <span class="n">replace_keywords</span><span class="o">=</span><span class="n">replace_keywords</span><span class="p">,</span>
                        <span class="n">strip</span><span class="o">=</span><span class="n">strip</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">old_data</span> <span class="o">!=</span> <span class="n">current_data</span><span class="p">:</span>
                    <span class="n">files_are_different</span> <span class="o">=</span> <span class="kc">True</span>

            <span class="k">if</span> <span class="n">old_data</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">files_are_different</span><span class="p">:</span>
                <span class="c1"># didn&#39;t have old data, assume we need to write it</span>
                <span class="c1"># or files were different</span>
                <span class="n">verbose</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Writing metadata with exiftool for </span><span class="si">{</span><span class="n">exported_file</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">dry_run</span><span class="p">:</span>
                    <span class="n">warning_</span><span class="p">,</span> <span class="n">error_</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_write_exif_data</span><span class="p">(</span>
                        <span class="n">exported_file</span><span class="p">,</span>
                        <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="n">use_albums_as_keywords</span><span class="p">,</span>
                        <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="n">use_persons_as_keywords</span><span class="p">,</span>
                        <span class="n">keyword_template</span><span class="o">=</span><span class="n">keyword_template</span><span class="p">,</span>
                        <span class="n">description_template</span><span class="o">=</span><span class="n">description_template</span><span class="p">,</span>
                        <span class="n">ignore_date_modified</span><span class="o">=</span><span class="n">ignore_date_modified</span><span class="p">,</span>
                        <span class="n">flags</span><span class="o">=</span><span class="n">exiftool_flags</span><span class="p">,</span>
                        <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="n">merge_exif_keywords</span><span class="p">,</span>
                        <span class="n">merge_exif_persons</span><span class="o">=</span><span class="n">merge_exif_persons</span><span class="p">,</span>
                        <span class="n">persons</span><span class="o">=</span><span class="n">persons</span><span class="p">,</span>
                        <span class="n">location</span><span class="o">=</span><span class="n">location</span><span class="p">,</span>
                        <span class="n">replace_keywords</span><span class="o">=</span><span class="n">replace_keywords</span><span class="p">,</span>
                        <span class="n">strip</span><span class="o">=</span><span class="n">strip</span><span class="p">,</span>
                    <span class="p">)</span>
                    <span class="k">if</span> <span class="n">warning_</span><span class="p">:</span>
                        <span class="n">all_results</span><span class="o">.</span><span class="n">exiftool_warning</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">exported_file</span><span class="p">,</span> <span class="n">warning_</span><span class="p">))</span>
                    <span class="k">if</span> <span class="n">error_</span><span class="p">:</span>
                        <span class="n">all_results</span><span class="o">.</span><span class="n">exiftool_error</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">exported_file</span><span class="p">,</span> <span class="n">error_</span><span class="p">))</span>
                        <span class="n">all_results</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">exported_file</span><span class="p">,</span> <span class="n">error_</span><span class="p">))</span>

                <span class="n">export_db</span><span class="o">.</span><span class="n">set_exifdata_for_file</span><span class="p">(</span>
                    <span class="n">exported_file</span><span class="p">,</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_exiftool_json_sidecar</span><span class="p">(</span>
                        <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="n">use_albums_as_keywords</span><span class="p">,</span>
                        <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="n">use_persons_as_keywords</span><span class="p">,</span>
                        <span class="n">keyword_template</span><span class="o">=</span><span class="n">keyword_template</span><span class="p">,</span>
                        <span class="n">description_template</span><span class="o">=</span><span class="n">description_template</span><span class="p">,</span>
                        <span class="n">ignore_date_modified</span><span class="o">=</span><span class="n">ignore_date_modified</span><span class="p">,</span>
                        <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="n">merge_exif_keywords</span><span class="p">,</span>
                        <span class="n">merge_exif_persons</span><span class="o">=</span><span class="n">merge_exif_persons</span><span class="p">,</span>
                        <span class="n">persons</span><span class="o">=</span><span class="n">persons</span><span class="p">,</span>
                        <span class="n">location</span><span class="o">=</span><span class="n">location</span><span class="p">,</span>
                        <span class="n">replace_keywords</span><span class="o">=</span><span class="n">replace_keywords</span><span class="p">,</span>
                        <span class="n">strip</span><span class="o">=</span><span class="n">strip</span><span class="p">,</span>
                    <span class="p">),</span>
                <span class="p">)</span>
                <span class="n">export_db</span><span class="o">.</span><span class="n">set_stat_exif_for_file</span><span class="p">(</span>
                    <span class="n">exported_file</span><span class="p">,</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">exported_file</span><span class="p">)</span>
                <span class="p">)</span>
                <span class="n">all_results</span><span class="o">.</span><span class="n">exif_updated</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exported_file</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">verbose</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Skipped up to date exiftool metadata for </span><span class="si">{</span><span class="n">exported_file</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">exiftool</span> <span class="ow">and</span> <span class="n">exif_files</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">exported_file</span> <span class="ow">in</span> <span class="n">exif_files</span><span class="p">:</span>
            <span class="n">verbose</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Writing metadata with exiftool for </span><span class="si">{</span><span class="n">exported_file</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">dry_run</span><span class="p">:</span>
                <span class="n">warning_</span><span class="p">,</span> <span class="n">error_</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_write_exif_data</span><span class="p">(</span>
                    <span class="n">exported_file</span><span class="p">,</span>
                    <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="n">use_albums_as_keywords</span><span class="p">,</span>
                    <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="n">use_persons_as_keywords</span><span class="p">,</span>
                    <span class="n">keyword_template</span><span class="o">=</span><span class="n">keyword_template</span><span class="p">,</span>
                    <span class="n">description_template</span><span class="o">=</span><span class="n">description_template</span><span class="p">,</span>
                    <span class="n">ignore_date_modified</span><span class="o">=</span><span class="n">ignore_date_modified</span><span class="p">,</span>
                    <span class="n">flags</span><span class="o">=</span><span class="n">exiftool_flags</span><span class="p">,</span>
                    <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="n">merge_exif_keywords</span><span class="p">,</span>
                    <span class="n">merge_exif_persons</span><span class="o">=</span><span class="n">merge_exif_persons</span><span class="p">,</span>
                    <span class="n">persons</span><span class="o">=</span><span class="n">persons</span><span class="p">,</span>
                    <span class="n">location</span><span class="o">=</span><span class="n">location</span><span class="p">,</span>
                    <span class="n">replace_keywords</span><span class="o">=</span><span class="n">replace_keywords</span><span class="p">,</span>
                    <span class="n">strip</span><span class="o">=</span><span class="n">strip</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="k">if</span> <span class="n">warning_</span><span class="p">:</span>
                    <span class="n">all_results</span><span class="o">.</span><span class="n">exiftool_warning</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">exported_file</span><span class="p">,</span> <span class="n">warning_</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">error_</span><span class="p">:</span>
                    <span class="n">all_results</span><span class="o">.</span><span class="n">exiftool_error</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">exported_file</span><span class="p">,</span> <span class="n">error_</span><span class="p">))</span>
                    <span class="n">all_results</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">exported_file</span><span class="p">,</span> <span class="n">error_</span><span class="p">))</span>

            <span class="n">export_db</span><span class="o">.</span><span class="n">set_exifdata_for_file</span><span class="p">(</span>
                <span class="n">exported_file</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_exiftool_json_sidecar</span><span class="p">(</span>
                    <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="n">use_albums_as_keywords</span><span class="p">,</span>
                    <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="n">use_persons_as_keywords</span><span class="p">,</span>
                    <span class="n">keyword_template</span><span class="o">=</span><span class="n">keyword_template</span><span class="p">,</span>
                    <span class="n">description_template</span><span class="o">=</span><span class="n">description_template</span><span class="p">,</span>
                    <span class="n">ignore_date_modified</span><span class="o">=</span><span class="n">ignore_date_modified</span><span class="p">,</span>
                    <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="n">merge_exif_keywords</span><span class="p">,</span>
                    <span class="n">merge_exif_persons</span><span class="o">=</span><span class="n">merge_exif_persons</span><span class="p">,</span>
                    <span class="n">persons</span><span class="o">=</span><span class="n">persons</span><span class="p">,</span>
                    <span class="n">location</span><span class="o">=</span><span class="n">location</span><span class="p">,</span>
                    <span class="n">replace_keywords</span><span class="o">=</span><span class="n">replace_keywords</span><span class="p">,</span>
                    <span class="n">strip</span><span class="o">=</span><span class="n">strip</span><span class="p">,</span>
                <span class="p">),</span>
            <span class="p">)</span>
            <span class="n">export_db</span><span class="o">.</span><span class="n">set_stat_exif_for_file</span><span class="p">(</span>
                <span class="n">exported_file</span><span class="p">,</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">exported_file</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="n">all_results</span><span class="o">.</span><span class="n">exif_updated</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exported_file</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">touch_file</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">exif_file</span> <span class="ow">in</span> <span class="n">all_results</span><span class="o">.</span><span class="n">exif_updated</span><span class="p">:</span>
            <span class="n">verbose</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Updating file modification time for </span><span class="si">{</span><span class="n">exif_file</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="n">all_results</span><span class="o">.</span><span class="n">touched</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exif_file</span><span class="p">)</span>
            <span class="n">ts</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">timestamp</span><span class="p">())</span>
            <span class="n">fileutil</span><span class="o">.</span><span class="n">utime</span><span class="p">(</span><span class="n">exif_file</span><span class="p">,</span> <span class="p">(</span><span class="n">ts</span><span class="p">,</span> <span class="n">ts</span><span class="p">))</span>

    <span class="n">all_results</span><span class="o">.</span><span class="n">touched</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">all_results</span><span class="o">.</span><span class="n">touched</span><span class="p">))</span>

    <span class="n">all_results</span><span class="o">.</span><span class="n">sidecar_json_written</span> <span class="o">=</span> <span class="n">sidecar_json_files_written</span>
    <span class="n">all_results</span><span class="o">.</span><span class="n">sidecar_json_skipped</span> <span class="o">=</span> <span class="n">sidecar_json_files_skipped</span>
    <span class="n">all_results</span><span class="o">.</span><span class="n">sidecar_exiftool_written</span> <span class="o">=</span> <span class="n">sidecar_exiftool_files_written</span>
    <span class="n">all_results</span><span class="o">.</span><span class="n">sidecar_exiftool_skipped</span> <span class="o">=</span> <span class="n">sidecar_exiftool_files_skipped</span>
    <span class="n">all_results</span><span class="o">.</span><span class="n">sidecar_xmp_written</span> <span class="o">=</span> <span class="n">sidecar_xmp_files_written</span>
    <span class="n">all_results</span><span class="o">.</span><span class="n">sidecar_xmp_skipped</span> <span class="o">=</span> <span class="n">sidecar_xmp_files_skipped</span>

    <span class="k">return</span> <span class="n">all_results</span>


<span class="k">def</span> <span class="nf">_export_photo_with_photos_export</span><span class="p">(</span>
    <span class="bp">self</span><span class="p">,</span>
    <span class="n">dest</span><span class="p">,</span>
    <span class="n">all_results</span><span class="p">,</span>
    <span class="n">fileutil</span><span class="p">,</span>
    <span class="n">export_db</span><span class="p">,</span>
    <span class="n">use_photokit</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">dry_run</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">timeout</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">jpeg_ext</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">touch_file</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">update</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">overwrite</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">live_photo</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">edited</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">convert_to_jpeg</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">jpeg_quality</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span>
<span class="p">):</span>
    <span class="c1"># TODO: duplicative code with the if edited/else--remove it</span>
    <span class="c1"># export live_photo .mov file?</span>
    <span class="n">live_photo</span> <span class="o">=</span> <span class="nb">bool</span><span class="p">(</span><span class="n">live_photo</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">live_photo</span><span class="p">)</span>
    <span class="n">overwrite</span> <span class="o">=</span> <span class="n">overwrite</span> <span class="ow">or</span> <span class="n">update</span>
    <span class="k">if</span> <span class="n">edited</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">shared</span><span class="p">:</span>
        <span class="c1"># exported edited version and not original</span>
        <span class="c1"># shared photos (in shared albums) show up as not having adjustments (not edited)</span>
        <span class="c1"># but Photos is unable to export the &quot;original&quot; as only a jpeg copy is shared in iCloud</span>
        <span class="c1"># so tell Photos to export the current version in this case</span>
        <span class="c1"># didn&#39;t get passed a filename, add _edited</span>
        <span class="n">uti</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">uti_edited</span> <span class="k">if</span> <span class="n">edited</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">uti_edited</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">uti</span>
        <span class="n">ext</span> <span class="o">=</span> <span class="n">get_preferred_uti_extension</span><span class="p">(</span><span class="n">uti</span><span class="p">)</span>
        <span class="n">dest</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}</span><span class="s2">.</span><span class="si">{</span><span class="n">ext</span><span class="si">}</span><span class="s2">&quot;</span>

        <span class="k">if</span> <span class="n">use_photokit</span><span class="p">:</span>
            <span class="n">photolib</span> <span class="o">=</span> <span class="n">PhotoLibrary</span><span class="p">()</span>
            <span class="n">photo</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">photo</span> <span class="o">=</span> <span class="n">photolib</span><span class="o">.</span><span class="n">fetch_uuid</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">uuid</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">PhotoKitFetchFailed</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="c1"># if failed to find UUID, might be a burst photo</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">burst</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_info</span><span class="p">[</span><span class="s2">&quot;burstUUID&quot;</span><span class="p">]:</span>
                    <span class="n">bursts</span> <span class="o">=</span> <span class="n">photolib</span><span class="o">.</span><span class="n">fetch_burst_uuid</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_info</span><span class="p">[</span><span class="s2">&quot;burstUUID&quot;</span><span class="p">],</span> <span class="nb">all</span><span class="o">=</span><span class="kc">True</span>
                    <span class="p">)</span>
                    <span class="c1"># PhotoKit UUIDs may contain &quot;/L0/001&quot; so only look at beginning</span>
                    <span class="n">photo</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">bursts</span> <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">uuid</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">uuid</span><span class="p">)]</span>
                    <span class="n">photo</span> <span class="o">=</span> <span class="n">photo</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="n">photo</span> <span class="k">else</span> <span class="kc">None</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">photo</span><span class="p">:</span>
                    <span class="n">all_results</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                        <span class="p">(</span>
                            <span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span>
                            <span class="sa">f</span><span class="s2">&quot;PhotoKitFetchFailed exception exporting photo </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">uuid</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">,</span>
                        <span class="p">)</span>
                    <span class="p">)</span>
            <span class="k">if</span> <span class="n">photo</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">dry_run</span><span class="p">:</span>
                    <span class="c1"># dry_run, don&#39;t actually export</span>
                    <span class="n">all_results</span><span class="o">.</span><span class="n">exported</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">exported</span> <span class="o">=</span> <span class="n">photo</span><span class="o">.</span><span class="n">export</span><span class="p">(</span>
                            <span class="n">dest</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span>
                            <span class="n">dest</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                            <span class="n">version</span><span class="o">=</span><span class="n">PHOTOS_VERSION_CURRENT</span><span class="p">,</span>
                            <span class="n">overwrite</span><span class="o">=</span><span class="n">overwrite</span><span class="p">,</span>
                            <span class="n">video</span><span class="o">=</span><span class="n">live_photo</span><span class="p">,</span>
                        <span class="p">)</span>
                        <span class="n">all_results</span><span class="o">.</span><span class="n">exported</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">exported</span><span class="p">)</span>
                    <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                        <span class="n">all_results</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">)</span>
                        <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">exported</span> <span class="o">=</span> <span class="n">_export_photo_uuid_applescript</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">uuid</span><span class="p">,</span>
                    <span class="n">dest</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span>
                    <span class="n">filestem</span><span class="o">=</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="p">,</span>
                    <span class="n">original</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                    <span class="n">edited</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                    <span class="n">live_photo</span><span class="o">=</span><span class="n">live_photo</span><span class="p">,</span>
                    <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span><span class="p">,</span>
                    <span class="n">burst</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">burst</span><span class="p">,</span>
                    <span class="n">dry_run</span><span class="o">=</span><span class="n">dry_run</span><span class="p">,</span>
                    <span class="n">overwrite</span><span class="o">=</span><span class="n">overwrite</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="n">all_results</span><span class="o">.</span><span class="n">exported</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">exported</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">ExportError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">all_results</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># export original version and not edited</span>
        <span class="k">if</span> <span class="n">use_photokit</span><span class="p">:</span>
            <span class="n">photolib</span> <span class="o">=</span> <span class="n">PhotoLibrary</span><span class="p">()</span>
            <span class="n">photo</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">photo</span> <span class="o">=</span> <span class="n">photolib</span><span class="o">.</span><span class="n">fetch_uuid</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">uuid</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">PhotoKitFetchFailed</span><span class="p">:</span>
                <span class="c1"># if failed to find UUID, might be a burst photo</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">burst</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_info</span><span class="p">[</span><span class="s2">&quot;burstUUID&quot;</span><span class="p">]:</span>
                    <span class="n">bursts</span> <span class="o">=</span> <span class="n">photolib</span><span class="o">.</span><span class="n">fetch_burst_uuid</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_info</span><span class="p">[</span><span class="s2">&quot;burstUUID&quot;</span><span class="p">],</span> <span class="nb">all</span><span class="o">=</span><span class="kc">True</span>
                    <span class="p">)</span>
                    <span class="c1"># PhotoKit UUIDs may contain &quot;/L0/001&quot; so only look at beginning</span>
                    <span class="n">photo</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">bursts</span> <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">uuid</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">uuid</span><span class="p">)]</span>
                    <span class="n">photo</span> <span class="o">=</span> <span class="n">photo</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="n">photo</span> <span class="k">else</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="n">photo</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">dry_run</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">exported</span> <span class="o">=</span> <span class="n">photo</span><span class="o">.</span><span class="n">export</span><span class="p">(</span>
                            <span class="n">dest</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span>
                            <span class="n">dest</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                            <span class="n">version</span><span class="o">=</span><span class="n">PHOTOS_VERSION_ORIGINAL</span><span class="p">,</span>
                            <span class="n">overwrite</span><span class="o">=</span><span class="n">overwrite</span><span class="p">,</span>
                            <span class="n">video</span><span class="o">=</span><span class="n">live_photo</span><span class="p">,</span>
                        <span class="p">)</span>
                        <span class="n">all_results</span><span class="o">.</span><span class="n">exported</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">exported</span><span class="p">)</span>
                    <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                        <span class="n">all_results</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                            <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">)</span>
                        <span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># dry_run, don&#39;t actually export</span>
                    <span class="n">all_results</span><span class="o">.</span><span class="n">exported</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">exported</span> <span class="o">=</span> <span class="n">_export_photo_uuid_applescript</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">uuid</span><span class="p">,</span>
                    <span class="n">dest</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span>
                    <span class="n">filestem</span><span class="o">=</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="p">,</span>
                    <span class="n">original</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                    <span class="n">edited</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                    <span class="n">live_photo</span><span class="o">=</span><span class="n">live_photo</span><span class="p">,</span>
                    <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span><span class="p">,</span>
                    <span class="n">burst</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">burst</span><span class="p">,</span>
                    <span class="n">dry_run</span><span class="o">=</span><span class="n">dry_run</span><span class="p">,</span>
                    <span class="n">overwrite</span><span class="o">=</span><span class="n">overwrite</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="n">all_results</span><span class="o">.</span><span class="n">exported</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">exported</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">ExportError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">all_results</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">all_results</span><span class="o">.</span><span class="n">exported</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">photopath</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">all_results</span><span class="o">.</span><span class="n">exported</span><span class="p">):</span>
            <span class="n">converted_stat</span> <span class="o">=</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
            <span class="n">photopath</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">photopath</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">convert_to_jpeg</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">isphoto</span><span class="p">:</span>
                <span class="c1"># if passed convert_to_jpeg=True, will assume the photo is a photo and not already a jpeg</span>
                <span class="k">if</span> <span class="n">photopath</span><span class="o">.</span><span class="n">suffix</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">LIVE_VIDEO_EXTENSIONS</span><span class="p">:</span>
                    <span class="n">dest_str</span> <span class="o">=</span> <span class="n">photopath</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">photopath</span><span class="o">.</span><span class="n">stem</span><span class="si">}</span><span class="s2">.jpeg&quot;</span>
                    <span class="n">fileutil</span><span class="o">.</span><span class="n">convert_to_jpeg</span><span class="p">(</span>
                        <span class="n">photopath</span><span class="p">,</span> <span class="n">dest_str</span><span class="p">,</span> <span class="n">compression_quality</span><span class="o">=</span><span class="n">jpeg_quality</span>
                    <span class="p">)</span>
                    <span class="n">converted_stat</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
                    <span class="n">fileutil</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">photopath</span><span class="p">)</span>
                    <span class="n">all_results</span><span class="o">.</span><span class="n">exported</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">dest_str</span>
                    <span class="n">all_results</span><span class="o">.</span><span class="n">converted_to_jpeg</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
                    <span class="n">photopath</span> <span class="o">=</span> <span class="n">dest_str</span>

            <span class="n">photopath</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">photopath</span><span class="p">)</span>
            <span class="n">export_db</span><span class="o">.</span><span class="n">set_data</span><span class="p">(</span>
                <span class="n">filename</span><span class="o">=</span><span class="n">photopath</span><span class="p">,</span>
                <span class="n">uuid</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">uuid</span><span class="p">,</span>
                <span class="n">orig_stat</span><span class="o">=</span><span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">photopath</span><span class="p">),</span>
                <span class="n">exif_stat</span><span class="o">=</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
                <span class="n">converted_stat</span><span class="o">=</span><span class="n">converted_stat</span><span class="p">,</span>
                <span class="n">edited_stat</span><span class="o">=</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
                <span class="n">info_json</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">json</span><span class="p">(),</span>
                <span class="n">exif_json</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
            <span class="p">)</span>

            <span class="c1"># todo: handle signatures</span>
        <span class="k">if</span> <span class="n">jpeg_ext</span><span class="p">:</span>
            <span class="c1"># use_photos_export (both PhotoKit and AppleScript) don&#39;t use the</span>
            <span class="c1"># file extension provided (instead they use extension for UTI)</span>
            <span class="c1"># so if jpeg_ext is set, rename any non-conforming jpegs</span>
            <span class="n">all_results</span><span class="o">.</span><span class="n">exported</span> <span class="o">=</span> <span class="n">rename_jpeg_files</span><span class="p">(</span>
                <span class="n">all_results</span><span class="o">.</span><span class="n">exported</span><span class="p">,</span> <span class="n">jpeg_ext</span><span class="p">,</span> <span class="n">fileutil</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">touch_file</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">exported_file</span> <span class="ow">in</span> <span class="n">all_results</span><span class="o">.</span><span class="n">exported</span><span class="p">:</span>
                <span class="n">all_results</span><span class="o">.</span><span class="n">touched</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">exported_file</span><span class="p">)</span>
                <span class="n">ts</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">timestamp</span><span class="p">())</span>
                <span class="n">fileutil</span><span class="o">.</span><span class="n">utime</span><span class="p">(</span><span class="n">exported_file</span><span class="p">,</span> <span class="p">(</span><span class="n">ts</span><span class="p">,</span> <span class="n">ts</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">update</span><span class="p">:</span>
            <span class="n">all_results</span><span class="o">.</span><span class="n">new</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">all_results</span><span class="o">.</span><span class="n">exported</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_export_photo</span><span class="p">(</span>
    <span class="bp">self</span><span class="p">,</span>
    <span class="n">src</span><span class="p">,</span>
    <span class="n">dest</span><span class="p">,</span>
    <span class="n">update</span><span class="p">,</span>
    <span class="n">export_db</span><span class="p">,</span>
    <span class="n">overwrite</span><span class="p">,</span>
    <span class="n">export_as_hardlink</span><span class="p">,</span>
    <span class="n">exiftool</span><span class="p">,</span>
    <span class="n">touch_file</span><span class="p">,</span>
    <span class="n">convert_to_jpeg</span><span class="p">,</span>
    <span class="n">fileutil</span><span class="o">=</span><span class="n">FileUtil</span><span class="p">,</span>
    <span class="n">edited</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">jpeg_quality</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span>
    <span class="n">ignore_signature</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Helper function for export()</span>
<span class="sd">        Does the actual copy or hardlink taking the appropriate</span>
<span class="sd">        action depending on update, overwrite, export_as_hardlink</span>
<span class="sd">        Assumes destination is the right destination (e.g. UUID matches)</span>
<span class="sd">        sets UUID and JSON info foo exported file using set_uuid_for_file, set_inf_for_uuido</span>

<span class="sd">    Args:</span>
<span class="sd">        src: src path (string)</span>
<span class="sd">        dest: dest path (pathlib.Path)</span>
<span class="sd">        update: bool</span>
<span class="sd">        export_db: instance of ExportDB that conforms to ExportDB_ABC interface</span>
<span class="sd">        overwrite: bool</span>
<span class="sd">        export_as_hardlink: bool</span>
<span class="sd">        exiftool: bool</span>
<span class="sd">        touch_file: bool</span>
<span class="sd">        convert_to_jpeg: bool; if True, convert file to jpeg on export</span>
<span class="sd">        fileutil: FileUtil class that conforms to fileutil.FileUtilABC</span>
<span class="sd">        edited: bool; set to True if exporting edited version of photo</span>
<span class="sd">        jpeg_quality: float in range 0.0 &lt;= jpeg_quality &lt;= 1.0.  A value of 1.0 specifies use best quality, a value of 0.0 specifies use maximum compression.</span>
<span class="sd">        ignore_signature: bool, ignore file signature when used with update (look only at filename)</span>

<span class="sd">    Returns:</span>
<span class="sd">        ExportResults</span>

<span class="sd">    Raises:</span>
<span class="sd">        ValueError if export_as_hardlink and convert_to_jpeg both True</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="n">export_as_hardlink</span> <span class="ow">and</span> <span class="n">convert_to_jpeg</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;export_as_hardlink and convert_to_jpeg cannot both be True&quot;</span><span class="p">)</span>

    <span class="n">exported_files</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">update_updated_files</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">update_new_files</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">update_skipped_files</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">touched_files</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">converted_to_jpeg_files</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="n">dest_str</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
    <span class="n">dest_exists</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">update</span><span class="p">:</span>  <span class="c1"># updating</span>
        <span class="n">cmp_touch</span><span class="p">,</span> <span class="n">cmp_orig</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="n">dest_exists</span><span class="p">:</span>
            <span class="c1"># update, destination exists, but we might not need to replace it...</span>
            <span class="k">if</span> <span class="n">ignore_signature</span><span class="p">:</span>
                <span class="n">cmp_orig</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="n">cmp_touch</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">mtime1</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">timestamp</span><span class="p">()))</span>
            <span class="k">elif</span> <span class="n">exiftool</span><span class="p">:</span>
                <span class="n">sig_exif</span> <span class="o">=</span> <span class="n">export_db</span><span class="o">.</span><span class="n">get_stat_exif_for_file</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
                <span class="n">cmp_orig</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp_file_sig</span><span class="p">(</span><span class="n">dest_str</span><span class="p">,</span> <span class="n">sig_exif</span><span class="p">)</span>
                <span class="n">sig_exif</span> <span class="o">=</span> <span class="p">(</span><span class="n">sig_exif</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">sig_exif</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">timestamp</span><span class="p">()))</span>
                <span class="n">cmp_touch</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp_file_sig</span><span class="p">(</span><span class="n">dest_str</span><span class="p">,</span> <span class="n">sig_exif</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">convert_to_jpeg</span><span class="p">:</span>
                <span class="n">sig_converted</span> <span class="o">=</span> <span class="n">export_db</span><span class="o">.</span><span class="n">get_stat_converted_for_file</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
                <span class="n">cmp_orig</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp_file_sig</span><span class="p">(</span><span class="n">dest_str</span><span class="p">,</span> <span class="n">sig_converted</span><span class="p">)</span>
                <span class="n">sig_converted</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="n">sig_converted</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                    <span class="n">sig_converted</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span>
                    <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">timestamp</span><span class="p">()),</span>
                <span class="p">)</span>
                <span class="n">cmp_touch</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp_file_sig</span><span class="p">(</span><span class="n">dest_str</span><span class="p">,</span> <span class="n">sig_converted</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">cmp_orig</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">)</span>
                <span class="n">cmp_touch</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">mtime1</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">timestamp</span><span class="p">()))</span>

            <span class="n">sig_cmp</span> <span class="o">=</span> <span class="n">cmp_touch</span> <span class="k">if</span> <span class="n">touch_file</span> <span class="k">else</span> <span class="n">cmp_orig</span>

            <span class="k">if</span> <span class="n">edited</span><span class="p">:</span>
                <span class="c1"># requested edited version of photo</span>
                <span class="c1"># need to see if edited version in Photos library has changed</span>
                <span class="c1"># (e.g. it&#39;s been edited again)</span>
                <span class="n">sig_edited</span> <span class="o">=</span> <span class="n">export_db</span><span class="o">.</span><span class="n">get_stat_edited_for_file</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
                <span class="n">cmp_edited</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp_file_sig</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">sig_edited</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">sig_edited</span> <span class="o">!=</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
                    <span class="k">else</span> <span class="kc">False</span>
                <span class="p">)</span>
                <span class="n">sig_cmp</span> <span class="o">=</span> <span class="n">sig_cmp</span> <span class="ow">and</span> <span class="n">cmp_edited</span>

            <span class="k">if</span> <span class="p">(</span><span class="n">export_as_hardlink</span> <span class="ow">and</span> <span class="n">dest</span><span class="o">.</span><span class="n">samefile</span><span class="p">(</span><span class="n">src</span><span class="p">))</span> <span class="ow">or</span> <span class="p">(</span>
                <span class="ow">not</span> <span class="n">export_as_hardlink</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">dest</span><span class="o">.</span><span class="n">samefile</span><span class="p">(</span><span class="n">src</span><span class="p">)</span> <span class="ow">and</span> <span class="n">sig_cmp</span>
            <span class="p">):</span>
                <span class="c1"># destination exists and signatures match, skip it</span>
                <span class="n">update_skipped_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># destination exists but signature is different</span>
                <span class="k">if</span> <span class="n">touch_file</span> <span class="ow">and</span> <span class="n">cmp_orig</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">cmp_touch</span><span class="p">:</span>
                    <span class="c1"># destination exists, signature matches original but does not match expected touch time</span>
                    <span class="c1"># skip exporting but update touch time</span>
                    <span class="n">update_skipped_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
                    <span class="n">touched_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
                <span class="k">elif</span> <span class="ow">not</span> <span class="n">touch_file</span> <span class="ow">and</span> <span class="n">cmp_touch</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">cmp_orig</span><span class="p">:</span>
                    <span class="c1"># destination exists, signature matches expected touch but not original</span>
                    <span class="c1"># user likely exported with touch_file and is now exporting without touch_file</span>
                    <span class="c1"># don&#39;t update the file because it&#39;s same but leave touch time</span>
                    <span class="n">update_skipped_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># destination exists but is different</span>
                    <span class="n">update_updated_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">touch_file</span><span class="p">:</span>
                        <span class="n">touched_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># update, destination doesn&#39;t exist (new file)</span>
            <span class="n">update_new_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">touch_file</span><span class="p">:</span>
                <span class="n">touched_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># not update, export the file</span>
        <span class="n">exported_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">touch_file</span><span class="p">:</span>
            <span class="n">sig</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">src</span><span class="p">)</span>
            <span class="n">sig</span> <span class="o">=</span> <span class="p">(</span><span class="n">sig</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">sig</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">timestamp</span><span class="p">()))</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp_file_sig</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">sig</span><span class="p">):</span>
                <span class="n">touched_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">update_skipped_files</span><span class="p">:</span>
        <span class="n">converted_stat</span> <span class="o">=</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="n">edited_stat</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">src</span><span class="p">)</span> <span class="k">if</span> <span class="n">edited</span> <span class="k">else</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">dest_exists</span> <span class="ow">and</span> <span class="p">(</span><span class="n">update</span> <span class="ow">or</span> <span class="n">overwrite</span><span class="p">):</span>
            <span class="c1"># need to remove the destination first</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">fileutil</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">ExportError</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Error removing file </span><span class="si">{</span><span class="n">dest</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> ((</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
                <span class="p">)</span> <span class="kn">from</span> <span class="nn">e</span>
        <span class="k">if</span> <span class="n">export_as_hardlink</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">fileutil</span><span class="o">.</span><span class="n">hardlink</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">ExportError</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Error hardlinking </span><span class="si">{</span><span class="n">src</span><span class="si">}</span><span class="s2"> to </span><span class="si">{</span><span class="n">dest</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
                <span class="p">)</span> <span class="kn">from</span> <span class="nn">e</span>
        <span class="k">elif</span> <span class="n">convert_to_jpeg</span><span class="p">:</span>
            <span class="c1"># use convert_to_jpeg to export the file</span>
            <span class="n">fileutil</span><span class="o">.</span><span class="n">convert_to_jpeg</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest_str</span><span class="p">,</span> <span class="n">compression_quality</span><span class="o">=</span><span class="n">jpeg_quality</span><span class="p">)</span>
            <span class="n">converted_stat</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
            <span class="n">converted_to_jpeg_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">fileutil</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest_str</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">ExportError</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Error copying file </span><span class="si">{</span><span class="n">src</span><span class="si">}</span><span class="s2"> to </span><span class="si">{</span><span class="n">dest_str</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
                <span class="p">)</span> <span class="kn">from</span> <span class="nn">e</span>

        <span class="n">export_db</span><span class="o">.</span><span class="n">set_data</span><span class="p">(</span>
            <span class="n">filename</span><span class="o">=</span><span class="n">dest_str</span><span class="p">,</span>
            <span class="n">uuid</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">uuid</span><span class="p">,</span>
            <span class="n">orig_stat</span><span class="o">=</span><span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">dest_str</span><span class="p">),</span>
            <span class="n">exif_stat</span><span class="o">=</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span>
            <span class="n">converted_stat</span><span class="o">=</span><span class="n">converted_stat</span><span class="p">,</span>
            <span class="n">edited_stat</span><span class="o">=</span><span class="n">edited_stat</span><span class="p">,</span>
            <span class="n">info_json</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">json</span><span class="p">(),</span>
            <span class="n">exif_json</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">if</span> <span class="n">touched_files</span><span class="p">:</span>
        <span class="n">ts</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">timestamp</span><span class="p">())</span>
        <span class="n">fileutil</span><span class="o">.</span><span class="n">utime</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="p">(</span><span class="n">ts</span><span class="p">,</span> <span class="n">ts</span><span class="p">))</span>

    <span class="k">return</span> <span class="n">ExportResults</span><span class="p">(</span>
        <span class="n">exported</span><span class="o">=</span><span class="n">exported_files</span> <span class="o">+</span> <span class="n">update_new_files</span> <span class="o">+</span> <span class="n">update_updated_files</span><span class="p">,</span>
        <span class="n">new</span><span class="o">=</span><span class="n">update_new_files</span><span class="p">,</span>
        <span class="n">updated</span><span class="o">=</span><span class="n">update_updated_files</span><span class="p">,</span>
        <span class="n">skipped</span><span class="o">=</span><span class="n">update_skipped_files</span><span class="p">,</span>
        <span class="n">exif_updated</span><span class="o">=</span><span class="p">[],</span>
        <span class="n">touched</span><span class="o">=</span><span class="n">touched_files</span><span class="p">,</span>
        <span class="n">converted_to_jpeg</span><span class="o">=</span><span class="n">converted_to_jpeg_files</span><span class="p">,</span>
        <span class="n">sidecar_json_written</span><span class="o">=</span><span class="p">[],</span>
        <span class="n">sidecar_json_skipped</span><span class="o">=</span><span class="p">[],</span>
        <span class="n">sidecar_exiftool_written</span><span class="o">=</span><span class="p">[],</span>
        <span class="n">sidecar_exiftool_skipped</span><span class="o">=</span><span class="p">[],</span>
        <span class="n">sidecar_xmp_written</span><span class="o">=</span><span class="p">[],</span>
        <span class="n">sidecar_xmp_skipped</span><span class="o">=</span><span class="p">[],</span>
        <span class="n">missing</span><span class="o">=</span><span class="p">[],</span>
        <span class="n">error</span><span class="o">=</span><span class="p">[],</span>
    <span class="p">)</span>


<span class="k">def</span> <span class="nf">_write_exif_data</span><span class="p">(</span>
    <span class="bp">self</span><span class="p">,</span>
    <span class="n">filepath</span><span class="p">,</span>
    <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">keyword_template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">description_template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">ignore_date_modified</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">flags</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">merge_exif_persons</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">persons</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">location</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">replace_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">strip</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;write exif data to image file at filepath</span>

<span class="sd">    Args:</span>
<span class="sd">        filepath: full path to the image file</span>
<span class="sd">        use_albums_as_keywords: treat album names as keywords</span>
<span class="sd">        use_persons_as_keywords: treat person names as keywords</span>
<span class="sd">        keyword_template: (list of strings); list of template strings to render as keywords</span>
<span class="sd">        ignore_date_modified: if True, sets EXIF:ModifyDate to EXIF:DateTimeOriginal even if date_modified is set</span>
<span class="sd">        flags: optional list of exiftool flags to prepend to exiftool command when writing metadata (e.g. -m or -F)</span>
<span class="sd">        persons: if True, write person data to metadata</span>
<span class="sd">        location: if True, write location data to metadata</span>
<span class="sd">        replace_keywords: if True, keyword_template replaces any keywords, otherwise it&#39;s additive</span>
<span class="sd">        strip: if True, strip any leading or trailing whitespace from rendered templates</span>

<span class="sd">    Returns:</span>
<span class="sd">        (warning, error) of warning and error strings if exiftool produces warnings or errors</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">filepath</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">FileNotFoundError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Could not find file </span><span class="si">{</span><span class="n">filepath</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
    <span class="n">exif_info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_exiftool_dict</span><span class="p">(</span>
        <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="n">use_albums_as_keywords</span><span class="p">,</span>
        <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="n">use_persons_as_keywords</span><span class="p">,</span>
        <span class="n">keyword_template</span><span class="o">=</span><span class="n">keyword_template</span><span class="p">,</span>
        <span class="n">description_template</span><span class="o">=</span><span class="n">description_template</span><span class="p">,</span>
        <span class="n">ignore_date_modified</span><span class="o">=</span><span class="n">ignore_date_modified</span><span class="p">,</span>
        <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="n">merge_exif_keywords</span><span class="p">,</span>
        <span class="n">merge_exif_persons</span><span class="o">=</span><span class="n">merge_exif_persons</span><span class="p">,</span>
        <span class="n">persons</span><span class="o">=</span><span class="n">persons</span><span class="p">,</span>
        <span class="n">location</span><span class="o">=</span><span class="n">location</span><span class="p">,</span>
        <span class="n">replace_keywords</span><span class="o">=</span><span class="n">replace_keywords</span><span class="p">,</span>
        <span class="n">strip</span><span class="o">=</span><span class="n">strip</span><span class="p">,</span>
    <span class="p">)</span>

    <span class="k">with</span> <span class="n">ExifTool</span><span class="p">(</span><span class="n">filepath</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="n">flags</span><span class="p">,</span> <span class="n">exiftool</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="o">.</span><span class="n">_exiftool_path</span><span class="p">)</span> <span class="k">as</span> <span class="n">exiftool</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">exiftag</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">exif_info</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="o">==</span> <span class="nb">list</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">val</span><span class="p">:</span>
                    <span class="n">exiftool</span><span class="o">.</span><span class="n">setvalue</span><span class="p">(</span><span class="n">exiftag</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">exiftool</span><span class="o">.</span><span class="n">setvalue</span><span class="p">(</span><span class="n">exiftag</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">exiftool</span><span class="o">.</span><span class="n">warning</span><span class="p">,</span> <span class="n">exiftool</span><span class="o">.</span><span class="n">error</span>


<span class="k">def</span> <span class="nf">_exiftool_dict</span><span class="p">(</span>
    <span class="bp">self</span><span class="p">,</span>
    <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">keyword_template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">description_template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">ignore_date_modified</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">merge_exif_persons</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">filename</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">persons</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">location</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">replace_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">strip</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Return dict of EXIF details for building exiftool JSON sidecar or sending commands to ExifTool.</span>
<span class="sd">        Does not include all the EXIF fields as those are likely already in the image.</span>

<span class="sd">    Args:</span>
<span class="sd">        filename: name of source image file (without path); if not None, exiftool JSON signature will be included; if None, signature will not be included</span>
<span class="sd">        use_albums_as_keywords: treat album names as keywords</span>
<span class="sd">        use_persons_as_keywords: treat person names as keywords</span>
<span class="sd">        keyword_template: (list of strings); list of template strings to render as keywords</span>
<span class="sd">        description_template: (list of strings); list of template strings to render for the description</span>
<span class="sd">        ignore_date_modified: if True, sets EXIF:ModifyDate to EXIF:DateTimeOriginal even if date_modified is set</span>
<span class="sd">        merge_exif_keywords: merge keywords in the file&#39;s exif metadata (requires exiftool)</span>
<span class="sd">        merge_exif_persons: merge persons in the file&#39;s exif metadata (requires exiftool)</span>
<span class="sd">        persons: if True, include person data</span>
<span class="sd">        location: if True, include location data</span>
<span class="sd">        replace_keywords: if True, keyword_template replaces any keywords, otherwise it&#39;s additive</span>
<span class="sd">        strip: if True, strip any rendered templates</span>

<span class="sd">    Returns: dict with exiftool tags / values</span>

<span class="sd">    Exports the following:</span>
<span class="sd">        EXIF:ImageDescription (may include template)</span>
<span class="sd">        XMP:Description (may include template)</span>
<span class="sd">        XMP:Title</span>
<span class="sd">        IPTC:ObjectName</span>
<span class="sd">        XMP:TagsList (may include album name, person name, or template)</span>
<span class="sd">        IPTC:Keywords (may include album name, person name, or template)</span>
<span class="sd">        IPTC:Caption-Abstract</span>
<span class="sd">        XMP:Subject (set to keywords + persons)</span>
<span class="sd">        XMP:PersonInImage</span>
<span class="sd">        EXIF:GPSLatitudeRef, EXIF:GPSLongitudeRef</span>
<span class="sd">        EXIF:GPSLatitude, EXIF:GPSLongitude</span>
<span class="sd">        EXIF:GPSPosition</span>
<span class="sd">        EXIF:DateTimeOriginal</span>
<span class="sd">        EXIF:OffsetTimeOriginal</span>
<span class="sd">        EXIF:ModifyDate</span>
<span class="sd">        IPTC:DateCreated</span>
<span class="sd">        IPTC:TimeCreated</span>
<span class="sd">        QuickTime:CreationDate</span>
<span class="sd">        QuickTime:CreateDate (UTC)</span>
<span class="sd">        QuickTime:ModifyDate (UTC)</span>
<span class="sd">        QuickTime:GPSCoordinates</span>
<span class="sd">        UserData:GPSCoordinates</span>

<span class="sd">    Reference:</span>
<span class="sd">        https://iptc.org/std/photometadata/specification/IPTC-PhotoMetadata-201610_1.pdf</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">exif</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">{</span>
            <span class="s2">&quot;SourceFile&quot;</span><span class="p">:</span> <span class="n">filename</span><span class="p">,</span>
            <span class="s2">&quot;ExifTool:ExifToolVersion&quot;</span><span class="p">:</span> <span class="s2">&quot;12.00&quot;</span><span class="p">,</span>
            <span class="s2">&quot;File:FileName&quot;</span><span class="p">:</span> <span class="n">filename</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="n">filename</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="k">else</span> <span class="p">{}</span>
    <span class="p">)</span>

    <span class="k">if</span> <span class="n">description_template</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">options</span> <span class="o">=</span> <span class="n">dataclasses</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_render_options</span><span class="p">,</span> <span class="n">expand_inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">inplace_sep</span><span class="o">=</span><span class="s2">&quot;, &quot;</span>
        <span class="p">)</span>
        <span class="n">rendered</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_template</span><span class="p">(</span><span class="n">description_template</span><span class="p">,</span> <span class="n">options</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">description</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">rendered</span><span class="p">)</span> <span class="k">if</span> <span class="n">rendered</span> <span class="k">else</span> <span class="s2">&quot;&quot;</span>
        <span class="k">if</span> <span class="n">strip</span><span class="p">:</span>
            <span class="n">description</span> <span class="o">=</span> <span class="n">description</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;EXIF:ImageDescription&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">description</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;XMP:Description&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">description</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;IPTC:Caption-Abstract&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">description</span>
    <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span><span class="p">:</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;EXIF:ImageDescription&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;XMP:Description&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;IPTC:Caption-Abstract&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span>

    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">title</span><span class="p">:</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;XMP:Title&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">title</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;IPTC:ObjectName&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">title</span>

    <span class="n">keyword_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">if</span> <span class="n">merge_exif_keywords</span><span class="p">:</span>
        <span class="n">keyword_list</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_exif_keywords</span><span class="p">())</span>

    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">keywords</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">replace_keywords</span><span class="p">:</span>
        <span class="n">keyword_list</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">keywords</span><span class="p">)</span>

    <span class="n">person_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">if</span> <span class="n">persons</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">merge_exif_persons</span><span class="p">:</span>
            <span class="n">person_list</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_exif_persons</span><span class="p">())</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">persons</span><span class="p">:</span>
            <span class="c1"># filter out _UNKNOWN_PERSON</span>
            <span class="n">person_list</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">p</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">persons</span> <span class="k">if</span> <span class="n">p</span> <span class="o">!=</span> <span class="n">_UNKNOWN_PERSON</span><span class="p">])</span>

        <span class="k">if</span> <span class="n">use_persons_as_keywords</span> <span class="ow">and</span> <span class="n">person_list</span><span class="p">:</span>
            <span class="n">keyword_list</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">person_list</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">use_albums_as_keywords</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">albums</span><span class="p">:</span>
        <span class="n">keyword_list</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">albums</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">keyword_template</span><span class="p">:</span>
        <span class="n">rendered_keywords</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">options</span> <span class="o">=</span> <span class="n">dataclasses</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_render_options</span><span class="p">,</span> <span class="n">none_str</span><span class="o">=</span><span class="n">_OSXPHOTOS_NONE_SENTINEL</span><span class="p">,</span> <span class="n">path_sep</span><span class="o">=</span><span class="s2">&quot;/&quot;</span>
        <span class="p">)</span>
        <span class="k">for</span> <span class="n">template_str</span> <span class="ow">in</span> <span class="n">keyword_template</span><span class="p">:</span>
            <span class="n">rendered</span><span class="p">,</span> <span class="n">unmatched</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_template</span><span class="p">(</span><span class="n">template_str</span><span class="p">,</span> <span class="n">options</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">unmatched</span><span class="p">:</span>
                <span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Unmatched template substitution for template: </span><span class="si">{</span><span class="n">template_str</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span><span class="n">unmatched</span><span class="si">}</span><span class="s2">&quot;</span>
                <span class="p">)</span>
            <span class="n">rendered_keywords</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">rendered</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">strip</span><span class="p">:</span>
            <span class="n">rendered_keywords</span> <span class="o">=</span> <span class="p">[</span><span class="n">keyword</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">rendered_keywords</span><span class="p">]</span>

        <span class="c1"># filter out any template values that didn&#39;t match by looking for sentinel</span>
        <span class="n">rendered_keywords</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">keyword</span>
            <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">rendered_keywords</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">_OSXPHOTOS_NONE_SENTINEL</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">keyword</span>
        <span class="p">]</span>

        <span class="c1"># check to see if any keywords too long</span>
        <span class="n">long_keywords</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">long_str</span>
            <span class="k">for</span> <span class="n">long_str</span> <span class="ow">in</span> <span class="n">rendered_keywords</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">long_str</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">_MAX_IPTC_KEYWORD_LEN</span>
        <span class="p">]</span>
        <span class="k">if</span> <span class="n">long_keywords</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_verbose</span><span class="p">(</span>
                <span class="sa">f</span><span class="s2">&quot;Warning: some keywords exceed max IPTC Keyword length of </span><span class="si">{</span><span class="n">_MAX_IPTC_KEYWORD_LEN</span><span class="si">}</span><span class="s2"> (exiftool will truncate these): </span><span class="si">{</span><span class="n">long_keywords</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="p">)</span>

        <span class="n">keyword_list</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">rendered_keywords</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">keyword_list</span><span class="p">:</span>
        <span class="c1"># remove duplicates</span>
        <span class="n">keyword_list</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">keyword</span><span class="p">)</span> <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">keyword_list</span><span class="p">])))</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;IPTC:Keywords&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">keyword_list</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;XMP:Subject&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">keyword_list</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;XMP:TagsList&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">keyword_list</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">persons</span> <span class="ow">and</span> <span class="n">person_list</span><span class="p">:</span>
        <span class="n">person_list</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">person_list</span><span class="p">)))</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;XMP:PersonInImage&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">person_list</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

    <span class="c1"># if self.favorite():</span>
    <span class="c1">#     exif[&quot;Rating&quot;] = 5</span>

    <span class="k">if</span> <span class="n">location</span><span class="p">:</span>
        <span class="p">(</span><span class="n">lat</span><span class="p">,</span> <span class="n">lon</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">location</span>
        <span class="k">if</span> <span class="n">lat</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">lon</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isphoto</span><span class="p">:</span>
                <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;EXIF:GPSLatitude&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">lat</span>
                <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;EXIF:GPSLongitude&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">lon</span>
                <span class="n">lat_ref</span> <span class="o">=</span> <span class="s2">&quot;N&quot;</span> <span class="k">if</span> <span class="n">lat</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="k">else</span> <span class="s2">&quot;S&quot;</span>
                <span class="n">lon_ref</span> <span class="o">=</span> <span class="s2">&quot;E&quot;</span> <span class="k">if</span> <span class="n">lon</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="k">else</span> <span class="s2">&quot;W&quot;</span>
                <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;EXIF:GPSLatitudeRef&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">lat_ref</span>
                <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;EXIF:GPSLongitudeRef&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">lon_ref</span>
            <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">ismovie</span><span class="p">:</span>
                <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;Keys:GPSCoordinates&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">lat</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span><span class="n">lon</span><span class="si">}</span><span class="s2">&quot;</span>
                <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;UserData:GPSCoordinates&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">lat</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span><span class="n">lon</span><span class="si">}</span><span class="s2">&quot;</span>

    <span class="c1"># process date/time and timezone offset</span>
    <span class="c1"># Photos exports the following fields and sets modify date to creation date</span>
    <span class="c1"># [EXIF]    Modify Date             : 2020:10:30 00:00:00</span>
    <span class="c1"># [EXIF]    Date/Time Original      : 2020:10:30 00:00:00</span>
    <span class="c1"># [EXIF]    Create Date             : 2020:10:30 00:00:00</span>
    <span class="c1"># [IPTC]    Digital Creation Date   : 2020:10:30</span>
    <span class="c1"># [IPTC]    Date Created            : 2020:10:30</span>
    <span class="c1">#</span>
    <span class="c1"># for videos:</span>
    <span class="c1"># [QuickTime]     CreateDate                      : 2020:12:11 06:10:10</span>
    <span class="c1"># [QuickTime]     ModifyDate                      : 2020:12:11 06:10:10</span>
    <span class="c1"># [Keys]          CreationDate                    : 2020:12:10 22:10:10-08:00</span>
    <span class="c1"># This code deviates from Photos in one regard:</span>
    <span class="c1"># if photo has modification date, use it otherwise use creation date</span>

    <span class="n">date</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">date</span>
    <span class="n">offsettime</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%z&quot;</span><span class="p">)</span>
    <span class="c1"># find timezone offset in format &quot;-04:00&quot;</span>
    <span class="n">offset</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;([+-]?)([\d]</span><span class="si">{2}</span><span class="s2">)([\d]</span><span class="si">{2}</span><span class="s2">)&quot;</span><span class="p">,</span> <span class="n">offsettime</span><span class="p">)</span>
    <span class="n">offset</span> <span class="o">=</span> <span class="n">offset</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>  <span class="c1"># findall returns list of tuples</span>
    <span class="n">offsettime</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">offset</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="si">}{</span><span class="n">offset</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s2">:</span><span class="si">{</span><span class="n">offset</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="si">}</span><span class="s2">&quot;</span>

    <span class="c1"># exiftool expects format to &quot;2015:01:18 12:00:00&quot;</span>
    <span class="n">datetimeoriginal</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y:%m:</span><span class="si">%d</span><span class="s2"> %H:%M:%S&quot;</span><span class="p">)</span>

    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isphoto</span><span class="p">:</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;EXIF:DateTimeOriginal&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">datetimeoriginal</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;EXIF:CreateDate&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">datetimeoriginal</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;EXIF:OffsetTimeOriginal&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">offsettime</span>

        <span class="n">dateoriginal</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y:%m:</span><span class="si">%d</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;IPTC:DateCreated&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">dateoriginal</span>

        <span class="n">timeoriginal</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;%H:%M:%S</span><span class="si">{</span><span class="n">offsettime</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;IPTC:TimeCreated&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">timeoriginal</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">date_modified</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">ignore_date_modified</span><span class="p">:</span>
            <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;EXIF:ModifyDate&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">date_modified</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y:%m:</span><span class="si">%d</span><span class="s2"> %H:%M:%S&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;EXIF:ModifyDate&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y:%m:</span><span class="si">%d</span><span class="s2"> %H:%M:%S&quot;</span><span class="p">)</span>
    <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">ismovie</span><span class="p">:</span>
        <span class="c1"># QuickTime spec specifies times in UTC</span>
        <span class="c1"># QuickTime:CreateDate and ModifyDate are in UTC w/ no timezone</span>
        <span class="c1"># QuickTime:CreationDate must include time offset or Photos shows invalid values</span>
        <span class="c1"># reference: https://exiftool.org/TagNames/QuickTime.html#Keys</span>
        <span class="c1">#            https://exiftool.org/forum/index.php?topic=11927.msg64369#msg64369</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;QuickTime:CreationDate&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">datetimeoriginal</span><span class="si">}{</span><span class="n">offsettime</span><span class="si">}</span><span class="s2">&quot;</span>

        <span class="n">date_utc</span> <span class="o">=</span> <span class="n">datetime_tz_to_utc</span><span class="p">(</span><span class="n">date</span><span class="p">)</span>
        <span class="n">creationdate</span> <span class="o">=</span> <span class="n">date_utc</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y:%m:</span><span class="si">%d</span><span class="s2"> %H:%M:%S&quot;</span><span class="p">)</span>
        <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;QuickTime:CreateDate&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">creationdate</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">date_modified</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">ignore_date_modified</span><span class="p">:</span>
            <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;QuickTime:ModifyDate&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">creationdate</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">exif</span><span class="p">[</span><span class="s2">&quot;QuickTime:ModifyDate&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">datetime_tz_to_utc</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">date_modified</span>
            <span class="p">)</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y:%m:</span><span class="si">%d</span><span class="s2"> %H:%M:%S&quot;</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">exif</span>


<span class="k">def</span> <span class="nf">_get_exif_keywords</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;returns list of keywords found in the file&#39;s exif metadata&quot;&quot;&quot;</span>
    <span class="n">keywords</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">exif</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">exiftool</span>
    <span class="k">if</span> <span class="n">exif</span><span class="p">:</span>
        <span class="n">exifdict</span> <span class="o">=</span> <span class="n">exif</span><span class="o">.</span><span class="n">asdict</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;IPTC:Keywords&quot;</span><span class="p">,</span> <span class="s2">&quot;XMP:TagsList&quot;</span><span class="p">,</span> <span class="s2">&quot;XMP:Subject&quot;</span><span class="p">]:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">kw</span> <span class="o">=</span> <span class="n">exifdict</span><span class="p">[</span><span class="n">field</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">kw</span> <span class="ow">and</span> <span class="nb">type</span><span class="p">(</span><span class="n">kw</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">list</span><span class="p">:</span>
                    <span class="n">kw</span> <span class="o">=</span> <span class="p">[</span><span class="n">kw</span><span class="p">]</span>
                <span class="n">kw</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">kw</span><span class="p">]</span>
                <span class="n">keywords</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">kw</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="k">pass</span>
    <span class="k">return</span> <span class="n">keywords</span>


<span class="k">def</span> <span class="nf">_get_exif_persons</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;returns list of persons found in the file&#39;s exif metadata&quot;&quot;&quot;</span>
    <span class="n">persons</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">exif</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">exiftool</span>
    <span class="k">if</span> <span class="n">exif</span><span class="p">:</span>
        <span class="n">exifdict</span> <span class="o">=</span> <span class="n">exif</span><span class="o">.</span><span class="n">asdict</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">p</span> <span class="o">=</span> <span class="n">exifdict</span><span class="p">[</span><span class="s2">&quot;XMP:PersonInImage&quot;</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">p</span> <span class="ow">and</span> <span class="nb">type</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">list</span><span class="p">:</span>
                <span class="n">p</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span><span class="p">]</span>
            <span class="n">p</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">p_</span><span class="p">)</span> <span class="k">for</span> <span class="n">p_</span> <span class="ow">in</span> <span class="n">p</span><span class="p">]</span>
            <span class="n">persons</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">pass</span>
    <span class="k">return</span> <span class="n">persons</span>


<span class="k">def</span> <span class="nf">_exiftool_json_sidecar</span><span class="p">(</span>
    <span class="bp">self</span><span class="p">,</span>
    <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">keyword_template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">description_template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">ignore_date_modified</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">tag_groups</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">merge_exif_persons</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">filename</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">persons</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">location</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">replace_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">strip</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Return dict of EXIF details for building exiftool JSON sidecar or sending commands to ExifTool.</span>
<span class="sd">        Does not include all the EXIF fields as those are likely already in the image.</span>

<span class="sd">    Args:</span>
<span class="sd">        use_albums_as_keywords: treat album names as keywords</span>
<span class="sd">        use_persons_as_keywords: treat person names as keywords</span>
<span class="sd">        keyword_template: (list of strings); list of template strings to render as keywords</span>
<span class="sd">        description_template: (list of strings); list of template strings to render for the description</span>
<span class="sd">        ignore_date_modified: if True, sets EXIF:ModifyDate to EXIF:DateTimeOriginal even if date_modified is set</span>
<span class="sd">        tag_groups: if True, tags are in form Group:TagName, e.g. IPTC:Keywords, otherwise group name is omitted, e.g. Keywords</span>
<span class="sd">        merge_exif_keywords: boolean; if True, merged keywords found in file&#39;s exif data (requires exiftool)</span>
<span class="sd">        merge_exif_persons: boolean; if True, merged persons found in file&#39;s exif data (requires exiftool)</span>
<span class="sd">        filename: filename of the destination image file for including in exiftool signature in JSON sidecar</span>
<span class="sd">        persons: if True, include person data</span>
<span class="sd">        location: if True, include location data</span>
<span class="sd">        replace_keywords: if True, keyword_template replaces any keywords, otherwise it&#39;s additive</span>
<span class="sd">        strip: if True, strip whitespace from rendered templates</span>

<span class="sd">    Returns: dict with exiftool tags / values</span>

<span class="sd">    Exports the following:</span>
<span class="sd">        EXIF:ImageDescription</span>
<span class="sd">        XMP:Description (may include template)</span>
<span class="sd">        IPTC:CaptionAbstract</span>
<span class="sd">        XMP:Title</span>
<span class="sd">        IPTC:ObjectName</span>
<span class="sd">        XMP:TagsList</span>
<span class="sd">        IPTC:Keywords (may include album name, person name, or template)</span>
<span class="sd">        XMP:Subject (set to keywords + person)</span>
<span class="sd">        XMP:PersonInImage</span>
<span class="sd">        EXIF:GPSLatitudeRef, EXIF:GPSLongitudeRef</span>
<span class="sd">        EXIF:GPSLatitude, EXIF:GPSLongitude</span>
<span class="sd">        EXIF:GPSPosition</span>
<span class="sd">        EXIF:DateTimeOriginal</span>
<span class="sd">        EXIF:OffsetTimeOriginal</span>
<span class="sd">        EXIF:ModifyDate</span>
<span class="sd">        IPTC:DigitalCreationDate</span>
<span class="sd">        IPTC:DateCreated</span>
<span class="sd">        QuickTime:CreationDate</span>
<span class="sd">        QuickTime:CreateDate (UTC)</span>
<span class="sd">        QuickTime:ModifyDate (UTC)</span>
<span class="sd">        QuickTime:GPSCoordinates</span>
<span class="sd">        UserData:GPSCoordinates</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">exif</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_exiftool_dict</span><span class="p">(</span>
        <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="n">use_albums_as_keywords</span><span class="p">,</span>
        <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="n">use_persons_as_keywords</span><span class="p">,</span>
        <span class="n">keyword_template</span><span class="o">=</span><span class="n">keyword_template</span><span class="p">,</span>
        <span class="n">description_template</span><span class="o">=</span><span class="n">description_template</span><span class="p">,</span>
        <span class="n">ignore_date_modified</span><span class="o">=</span><span class="n">ignore_date_modified</span><span class="p">,</span>
        <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="n">merge_exif_keywords</span><span class="p">,</span>
        <span class="n">merge_exif_persons</span><span class="o">=</span><span class="n">merge_exif_persons</span><span class="p">,</span>
        <span class="n">filename</span><span class="o">=</span><span class="n">filename</span><span class="p">,</span>
        <span class="n">persons</span><span class="o">=</span><span class="n">persons</span><span class="p">,</span>
        <span class="n">location</span><span class="o">=</span><span class="n">location</span><span class="p">,</span>
        <span class="n">replace_keywords</span><span class="o">=</span><span class="n">replace_keywords</span><span class="p">,</span>
        <span class="n">strip</span><span class="o">=</span><span class="n">strip</span><span class="p">,</span>
    <span class="p">)</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">tag_groups</span><span class="p">:</span>
        <span class="c1"># strip tag groups</span>
        <span class="n">exif_new</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">exif</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">k</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;.*:&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span>
            <span class="n">exif_new</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span>
        <span class="n">exif</span> <span class="o">=</span> <span class="n">exif_new</span>

    <span class="k">return</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">([</span><span class="n">exif</span><span class="p">])</span>


<span class="k">def</span> <span class="nf">_xmp_sidecar</span><span class="p">(</span>
    <span class="bp">self</span><span class="p">,</span>
    <span class="n">use_albums_as_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">use_persons_as_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">keyword_template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">description_template</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">extension</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">merge_exif_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">merge_exif_persons</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">persons</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">location</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="n">replace_keywords</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="n">strip</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;returns string for XMP sidecar</span>
<span class="sd">    use_albums_as_keywords: treat album names as keywords</span>
<span class="sd">    use_persons_as_keywords: treat person names as keywords</span>
<span class="sd">    keyword_template: (list of strings); list of template strings to render as keywords</span>
<span class="sd">    description_template: string; optional template string that will be rendered for use as photo description</span>
<span class="sd">    extension: which extension to use for SidecarForExtension property</span>
<span class="sd">    merge_exif_keywords: boolean; if True, merged keywords found in file&#39;s exif data (requires exiftool)</span>
<span class="sd">    merge_exif_persons: boolean; if True, merged persons found in file&#39;s exif data (requires exiftool)</span>
<span class="sd">    persons: if True, include person data</span>
<span class="sd">    location: if True, include location data</span>
<span class="sd">    replace_keywords: if True, keyword_template replaces any keywords, otherwise it&#39;s additive</span>
<span class="sd">    strip: if True, strip whitespace from rendered templates</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">xmp_template_file</span> <span class="o">=</span> <span class="p">(</span>
        <span class="n">_XMP_TEMPLATE_NAME</span> <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="o">.</span><span class="n">_beta</span> <span class="k">else</span> <span class="n">_XMP_TEMPLATE_NAME_BETA</span>
    <span class="p">)</span>
    <span class="n">xmp_template</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">_TEMPLATE_DIR</span><span class="p">,</span> <span class="n">xmp_template_file</span><span class="p">))</span>

    <span class="k">if</span> <span class="n">extension</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">extension</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">original_filename</span><span class="p">)</span>
        <span class="n">extension</span> <span class="o">=</span> <span class="n">extension</span><span class="o">.</span><span class="n">suffix</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="k">if</span> <span class="n">extension</span><span class="o">.</span><span class="n">suffix</span> <span class="k">else</span> <span class="kc">None</span>

    <span class="k">if</span> <span class="n">description_template</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">options</span> <span class="o">=</span> <span class="n">dataclasses</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_render_options</span><span class="p">,</span> <span class="n">expand_inplace</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">inplace_sep</span><span class="o">=</span><span class="s2">&quot;, &quot;</span>
        <span class="p">)</span>
        <span class="n">rendered</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_template</span><span class="p">(</span><span class="n">description_template</span><span class="p">,</span> <span class="n">options</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">description</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">rendered</span><span class="p">)</span> <span class="k">if</span> <span class="n">rendered</span> <span class="k">else</span> <span class="s2">&quot;&quot;</span>
        <span class="k">if</span> <span class="n">strip</span><span class="p">:</span>
            <span class="n">description</span> <span class="o">=</span> <span class="n">description</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">description</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="s2">&quot;&quot;</span>

    <span class="n">keyword_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">if</span> <span class="n">merge_exif_keywords</span><span class="p">:</span>
        <span class="n">keyword_list</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_exif_keywords</span><span class="p">())</span>

    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">keywords</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">replace_keywords</span><span class="p">:</span>
        <span class="n">keyword_list</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">keywords</span><span class="p">)</span>

    <span class="c1"># TODO: keyword handling in this and _exiftool_json_sidecar is</span>
    <span class="c1"># good candidate for pulling out in a function</span>

    <span class="n">person_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">if</span> <span class="n">persons</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">merge_exif_persons</span><span class="p">:</span>
            <span class="n">person_list</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_exif_persons</span><span class="p">())</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">persons</span><span class="p">:</span>
            <span class="c1"># filter out _UNKNOWN_PERSON</span>
            <span class="n">person_list</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">p</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">persons</span> <span class="k">if</span> <span class="n">p</span> <span class="o">!=</span> <span class="n">_UNKNOWN_PERSON</span><span class="p">])</span>

        <span class="k">if</span> <span class="n">use_persons_as_keywords</span> <span class="ow">and</span> <span class="n">person_list</span><span class="p">:</span>
            <span class="n">keyword_list</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">person_list</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">use_albums_as_keywords</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">albums</span><span class="p">:</span>
        <span class="n">keyword_list</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">albums</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">keyword_template</span><span class="p">:</span>
        <span class="n">rendered_keywords</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">options</span> <span class="o">=</span> <span class="n">dataclasses</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_render_options</span><span class="p">,</span> <span class="n">none_str</span><span class="o">=</span><span class="n">_OSXPHOTOS_NONE_SENTINEL</span><span class="p">,</span> <span class="n">path_sep</span><span class="o">=</span><span class="s2">&quot;/&quot;</span>
        <span class="p">)</span>
        <span class="k">for</span> <span class="n">template_str</span> <span class="ow">in</span> <span class="n">keyword_template</span><span class="p">:</span>
            <span class="n">rendered</span><span class="p">,</span> <span class="n">unmatched</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_template</span><span class="p">(</span><span class="n">template_str</span><span class="p">,</span> <span class="n">options</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">unmatched</span><span class="p">:</span>
                <span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Unmatched template substitution for template: </span><span class="si">{</span><span class="n">template_str</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span><span class="n">unmatched</span><span class="si">}</span><span class="s2">&quot;</span>
                <span class="p">)</span>
            <span class="n">rendered_keywords</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">rendered</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">strip</span><span class="p">:</span>
            <span class="n">rendered_keywords</span> <span class="o">=</span> <span class="p">[</span><span class="n">keyword</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">rendered_keywords</span><span class="p">]</span>

        <span class="c1"># filter out any template values that didn&#39;t match by looking for sentinel</span>
        <span class="n">rendered_keywords</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">keyword</span>
            <span class="k">for</span> <span class="n">keyword</span> <span class="ow">in</span> <span class="n">rendered_keywords</span>
            <span class="k">if</span> <span class="n">_OSXPHOTOS_NONE_SENTINEL</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">keyword</span>
        <span class="p">]</span>

        <span class="n">keyword_list</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">rendered_keywords</span><span class="p">)</span>

    <span class="c1"># remove duplicates</span>
    <span class="c1"># sorted mainly to make testing the XMP file easier</span>
    <span class="k">if</span> <span class="n">keyword_list</span><span class="p">:</span>
        <span class="n">keyword_list</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">keyword_list</span><span class="p">)))</span>
    <span class="k">if</span> <span class="n">persons</span> <span class="ow">and</span> <span class="n">person_list</span><span class="p">:</span>
        <span class="n">person_list</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">person_list</span><span class="p">)))</span>

    <span class="n">subject_list</span> <span class="o">=</span> <span class="n">keyword_list</span>

    <span class="k">if</span> <span class="n">location</span><span class="p">:</span>
        <span class="n">latlon</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">location</span>

    <span class="n">xmp_str</span> <span class="o">=</span> <span class="n">xmp_template</span><span class="o">.</span><span class="n">render</span><span class="p">(</span>
        <span class="n">photo</span><span class="o">=</span><span class="bp">self</span><span class="p">,</span>
        <span class="n">description</span><span class="o">=</span><span class="n">description</span><span class="p">,</span>
        <span class="n">keywords</span><span class="o">=</span><span class="n">keyword_list</span><span class="p">,</span>
        <span class="n">persons</span><span class="o">=</span><span class="n">person_list</span><span class="p">,</span>
        <span class="n">subjects</span><span class="o">=</span><span class="n">subject_list</span><span class="p">,</span>
        <span class="n">extension</span><span class="o">=</span><span class="n">extension</span><span class="p">,</span>
        <span class="n">location</span><span class="o">=</span><span class="n">latlon</span><span class="p">,</span>
        <span class="n">version</span><span class="o">=</span><span class="n">__version__</span><span class="p">,</span>
    <span class="p">)</span>

    <span class="c1"># remove extra lines that mako inserts from template</span>
    <span class="n">xmp_str</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">line</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">xmp_str</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span> <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">!=</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">xmp_str</span>


<span class="k">def</span> <span class="nf">_write_sidecar</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">sidecar_str</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;write sidecar_str to filename</span>
<span class="sd">    used for exporting sidecar info&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">filename</span> <span class="ow">or</span> <span class="n">sidecar_str</span><span class="p">):</span>
        <span class="k">raise</span> <span class="p">(</span>
            <span class="ne">ValueError</span><span class="p">(</span>
                <span class="sa">f</span><span class="s2">&quot;filename </span><span class="si">{</span><span class="n">filename</span><span class="si">}</span><span class="s2"> and sidecar_str </span><span class="si">{</span><span class="n">sidecar_str</span><span class="si">}</span><span class="s2"> must not be None&quot;</span>
            <span class="p">)</span>
        <span class="p">)</span>

    <span class="c1"># TODO: catch exception?</span>
    <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span>
    <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">sidecar_str</span><span class="p">)</span>
    <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../../index.html">osxphotos</a></h1>








<h3>Navigation</h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../cli.html">osxphotos command line interface (CLI)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference.html">osxphotos package</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../index.html">Documentation overview</a><ul>
  <li><a href="../../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2021, Rhet Turnbull.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 4.2.0</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
      
    </div>

    

    
  </body>
</html>